Leanpub Header

Skip to main content

If you can't find what you need here, see the Leanpub FAQ, the Leanpub Author FAQ, The Leanpub Manual, The Leanpub Authors Forum, The Markua Manual or The Leanpub Flavoured Markdown Manual. If none of those help, please Contact Us.


The Leanpub API

Terms

Using the Leanpub API requires a Pro plan. To upgrade to a Pro plan, please go here.

All use of the Leanpub API must be authenticated with an API key.

Access to the Leanpub API may be revoked for any user, at any time, for any reason. In other words, please don't hammer our servers :)

Authentication

Getting your API key

You need to get an API key to start using the API. Go here to get one. Note that you typically need a Pro plan to get an API key. You can also get an API key if you are the author of a book for an organization which has an Organization Pro Plan, but in that case your API key will only work for that book.

Click on the button that says "Enable the Leanpub API" and copy the API key.

Using your API key

You need to send your API key on every API request you make. For GET requests, append it as a query string with a key of api_key:

GET https://leanpub.com/some/path?api_key=YOUR_API_KEY

For posts, you can also include the API key as part of the post data with a key of api_key. Here's an example in cURL:

curl -d "api_key=YOUR_API_KEY" https://leanpub.com/some/path

Your book's slug

You'll need to know your book's slug. It's the part of the URL for your book after https://leanpub.com/. E.g. if your book is found at https://leanpub.com/your_book, then your book's slug is your_book.

Previewing and Publishing

Previewing

To start a preview of your book, you POST to https://leanpub.com/SLUG/preview.json passing api_key as part of the post data. Here's an example with cURL:

curl -d "api_key=YOUR_API_KEY" https://leanpub.com/SLUG/preview.json

To preview a subset of your book using Subset.txt, post to https://leanpub.com/SLUG/preview/subset.json, passing the api_key as usual. A subset preview only generates a PDF, to go as fast as possible.

curl -d "api_key=YOUR_API_KEY" https://leanpub.com/SLUG/preview/subset.json

Previewing a single file

Sometimes you just want to take a look at the file you are working on and don't want to mess around with Subset.txt or anything like that. You can do that with the Single File generation API.

To use it, make a post to https://leanpub.com/SLUG/preview/single.json with a Content-Type header of text/plain and the body of the post containing the Markdown you want to generate. We will place the resulting PDF in the previews folder of your Dropbox, with a name of SLUG-single-file.pdf.

Getting the newlines working when posting with CURL is a bit complicated, so we'll use a Ruby script as an example this time. To use it, set an environment variable called LEANPUB_API_KEY to your Leanpub API key:

export LEANPUB_API_KEY="yourapikey"

Then call the script like this:

./generate_single_file <book_slug> <path_to_file>

e.g.

./generate_single_file thes3cookbook /path/to/some/markdown.txt

here's the script:

#!/usr/bin/env ruby
require "httpclient"

slug = ARGV[0]
filename = ARGV[1]
api_key = ENV["LEANPUB_API_KEY"]

content = File.read(filename)
headers = { "Content-Type" => "text/plain"}

url = "https://leanpub.com/#{slug}/preview/single.json?api_key=#{api_key}"
HTTPClient.new.post(url, :body => content, :header => headers)

Publishing

To publish your book, you POST to https://leanpub.com/SLUG/publish.json with your api key as part of the post data. This will publish without emailing your readers. Here's an example with cURL:

curl -d "api_key=YOUR_API_KEY" https://leanpub.com/SLUG/publish.json

If you want to email readers and add some release notes, add keys for publish[email_readers] and publish[release_notes]. The release notes should be URL encoded.

Getting the job status

Once you have started a preview or publish, you can get the status of the current job for your book by doing a GET on https://leanpub.com/SLUG/job_status.json with your api key as a query param:

curl "https://leanpub.com/SLUG/job_status?api_key=YOUR_API_KEY"

The result is a JSON response that looks like this:

{
  "num": 8,
  "job_type": "GenerateBookJob#preview",
  "total": 28,
  "message": "Downloading organization logo...",
  "status": "working",
  "name": "Publish scotttest99",
  "time": 1376073552,
  "options": {
    "requested_by": "peter@leanpub.com",
    "release_notes": "this is a test\n\nwith two lines",
    "slug": "scotttest99",
    "action": "publish",
    "email_readers": true
  }
}

Getting Book Summary Information

You can get information about a book using curl with your API key:

curl https://leanpub.com/SLUG.json?api_key=YOUR_API_KEY

Like all Leanpub API calls, this requires authentication. We only show you information about books where you are one of the authors (or if the book is published by an organization, and you are one of the editors).

Getting sales data

Summary Sales Data

You can get a summary of your sales and royalties at

https://leanpub.com/SLUG/royalties.json

or

https://leanpub.com/SLUG/royalties.xml

Here's an example with curl:

curl "https://leanpub.com/SLUG/royalties.json?api_key=YOUR_API_KEY"

All Sales Data

You can access data for all of your individualy purchases in JSON and XML format by doing a GET request to

https://leanpub.com/SLUG/individual_purchases.json

or

https://leanpub.com/SLUG/individual_purchases.xml

This data is paginated, and by default gives you your last 50 sales. Here's an example to get the last 50 sales of your book:

curl "https://leanpub.com/SLUG/individual_purchases.json?api_key=YOUR_API_KEY"

If you want to get the next 50 sales, request page 2 of your sales data like this:

curl "https://leanpub.com/SLUG/individual_purchases.json?api_key=YOUR_API_KEY&page=2"

Coupons

You can get information about the coupons for a given book and create new coupons for a book.

Getting a list of coupons for a book

To get a list of coupons, make a GET request to

https://leanpub.com/SLUG/coupons.json

or

https://leanpub.com/SLUG/coupons.xml

The response will include a list of all coupons for your book. Here is an example JSON response for a book with a single coupon:

[
  {
    "coupon_code": "NOT_A_REAL_COUPON",
    "created_at": "2013-04-17T22:12:58Z",
    "package_discounts": [
      {
        "package_slug": "book",
        "discounted_price": 2.0
      },
      {
        "package_slug": "teamedition",
        "discounted_price": 4.0
      }
    ],
    "end_date": "2016-05-17",
    "max_uses": null,
    "note": "This is not a real coupon",
    "num_uses": 12,
    "start_date": "2013-04-17",
    "suspended": false,
    "updated_at": "2013-04-17T22:12:58Z",
    "book_slug": "yourbook"
  }
]

Creating coupons

You can create coupons by POSTing to https://leanpub.com/SLUG/coupons.json.

You need to send the following data:

... (truncated for brevity) ...