Markdown to Kindle: EPUB, MOBI and AZW3 via API

Chain two API calls, Markdown to HTML and then HTML to EPUB, MOBI or AZW3, with a title, author and cover.

DocGen has no one-shot Markdown-to-ebook endpoint. You make two calls and feed the first result into the second: /api/v1/markdown turns GitHub-flavored Markdown into HTML, then /api/v1/epub, /mobi or /azw3 packages that HTML as an ebook. Each call is one conversion, so a book costs two.

Which format?

  • EPUB is the open standard. Apple Books, Kobo and most reading apps open it, and Amazon's Send to Kindle accepts it.
  • AZW3 (KF8) is Kindle's own format. Copy it to a Kindle over USB.
  • MOBI is the older Kindle format, for older devices and apps.

1. Markdown to HTML

Send the Markdown in the markdown field; css is optional. Tables, task lists, strikethrough and autolinks work, and raw HTML inside the Markdown passes through. The response is the HTML itself (text/html), so save it straight to a file:

jq -n --rawfile md book.md '{markdown: $md}' |
  curl -sS --fail-with-body -X POST "https://docgen.novtopro.com/api/v1/markdown" \
    -H "Authorization: Bearer $DOCGEN_API_KEY" \
    -H "Content-Type: application/json" \
    --data-binary @- -o book.html

2. HTML to ebook

Send the HTML with the book's metadata. title is required: without it, or with an empty one, the call fails with 400 title_required. author and css are optional.

jq -n --rawfile html book.html '{html: $html, title: "Field Notes", author: "Ada Lovelace"}' |
  curl -sS --fail-with-body -X POST "https://docgen.novtopro.com/api/v1/epub" \
    -H "Authorization: Bearer $DOCGEN_API_KEY" \
    -H "Content-Type: application/json" \
    --data-binary @- -o book.epub

For the Kindle formats, swap /api/v1/epub for /api/v1/azw3 or /api/v1/mobi and change the file name; the body stays the same.

3. Add a cover

The optional cover field takes the image bytes as base64: JPEG, PNG, GIF or WebP. Line breaks in the base64 are ignored, so the output of the base64 command works as is. A cover that can't be decoded fails the conversion with 502 conversion_failed instead of producing a book without one.

base64 < cover.jpg > cover.b64
jq -n --rawfile html book.html --rawfile cover cover.b64 \
  '{html: $html, title: "Field Notes", author: "Ada Lovelace", cover: $cover}' |
  curl -sS --fail-with-body -X POST "https://docgen.novtopro.com/api/v1/azw3" \
    -H "Authorization: Bearer $DOCGEN_API_KEY" \
    -H "Content-Type: application/json" \
    --data-binary @- -o book.azw3

The cover counts toward the request body limit: 256 KB on the free plan, 4 MB on Pro. Shrink large images first.

Good to know

  • Markdown is accepted inline only, and the ebook endpoints take html, not url.
  • Page size, orientation and margins apply to PDF only; the ebook endpoints ignore them.
  • The same HTML also works for /api/v1/pdf if you want a printable copy.
  • No JavaScript runs and nothing is stored, so keep the files the API returns. Every call draws from the same monthly conversions as PDFs.

API docs → · Guides