Getting started
github.com/Serendipity-AI/markdown2pdf-typescript Our Typescript SDK makes it easy to use markdown2pdf.ai in your agentic workflows. Whilst using our REST APIs is straightforward, there are some complexities (such as polling for payment completion and document generation) that the SDK handles for you. First, install the SDK:If you run your TypeScript or ES module file (for example, via
node test.ts
) and see a warning like Module type of file is not specified and it doesn’t parse as CommonJS, you can add a "type": "module"
(or "type": "commonjs"
) in your package.json
(or rename your file to .mjs
) to remove the warning.onPaymentRequest
callback (here our pay
function) so that you can handle the payment (for example, by printing the invoice and waiting for manual confirmation). You can also integrate with services like fewsats.com or albyhub for automatic payments.
SDK Reference
Types
The SDK exports the following types (and classes) (from “@serendipityai/markdown2pdf-typescript”):A TypeScript type that describes the payment offer details. This type is used in the Example usage:
onPaymentRequest
callback.A TypeScript type that describes the configuration options for the Example usage:
convertMarkdownToPdf
function.- Markdown2PdfError – A class (extending Error) thrown (or used) for general conversion errors.
- PaymentRequiredError – A class (extending Markdown2PdfError) thrown (or used) if a payment is required but no “onPaymentRequest” callback is provided.
Function: convertMarkdownToPdf
Signature:markdown
: The markdown content to convertoptions
: An optional object containing configuration parameters for the conversion process
onPaymentRequest
callback so that you can handle the payment. If onPaymentRequest
is omitted and a payment is required, a PaymentRequiredError
is thrown.The SDK retrieves the final PDF; either saving it to
downloadPath
or returning it as a Buffer
if returnBytes
is true
.
Parameters:
A string containing the markdown content you want to convert to PDF.For example:
A note on markdown hashingmarkdown2pdf.ai uses a hashing mechanism to ensure that you won’t get charged twice for creating the same PDF. Subsequent requests to convert the same markdown content will return the previously generated PDF without requiring a new payment.
A callback function that is called when a Lightning payment is required. The function receives an
offer
object containing payment details. This parameter is required because the service uses the L402 protocol, which requires a payment to be made before generating the PDF.A string containing the date to use on the front cover. If not provided, the current date will be used.For example:
A string containing the title to use on the front cover. If not provided, a default will be used.For example:
A string containing the path to use for saving the PDF file. If not provided and returnBytes is false, the PDF URL will be returned instead.For example:
Whether or not to return the PDF as bytes instead of saving it to a file. If set to
true
, the method will return the PDF content as a Buffer. If set to false
, it will save the PDF to the specified downloadPath
or return the URL if no path is provided.For example:This parameter enables you to override the default API URL. This is typically used in development or testing environments, but for normal usage you won’t need to touch it and it can be set to
undefined
or omitted.Condition | Resolves to | Type |
---|---|---|
downloadPath is provided and returnBytes is false | The value of downloadPath | string |
returnBytes is true | The PDF as a Buffer | Buffer |
Neither downloadPath nor returnBytes is provided/true | The final download URL | string |
An error occurs (network, timeout, or payment required and no callback) | Rejects with error | Markdown2PdfError or PaymentRequiredError |
Promise
that resolves or rejects as described above.
Automating payments
If you want to automate Lightning payments (so that your “onPaymentRequest” callback pays the invoice automatically), you can integrate with a service (or Lightning wallet) such as fewsats.com or albyhub. (These services (or wallets) allow you to pay Lightning invoices “headlessly”.)Below are two example integrations (using “fewsats” (via the Fewsats SDK) and “alby” (via axios) respectively):