Sign In

@docapi/sdk

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@docapi/sdk

Official Node.js SDK for DocAPI — generate PDFs and screenshots from HTML

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

docapi

Official Node.js SDK for DocAPI — generate PDFs and screenshots from HTML.

npm install @docapi/sdk

Zero dependencies. Uses native fetch (Node.js 18+).

Quick start

import { DocAPI } from "@docapi/sdk";

const client = new DocAPI("pk_live_...");

// Generate a PDF
const pdf = await client.pdf("<h1>Hello World</h1>");
fs.writeFileSync("output.pdf", pdf);

// Screenshot a URL
const img = await client.screenshot({ url: "https://example.com" });
fs.writeFileSync("screenshot.png", img);

Get a free API key at docapi.co/signup — 100 calls/month, no credit card.

API

new DocAPI(apiKey)

const client = new DocAPI("pk_live_...");

client.pdf(html, options?)

Convert HTML to a PDF. Returns a Buffer.

const pdf = await client.pdf(`
  <html>
    <head>
      <style>
        body { font-family: sans-serif; padding: 40px; }
        h1 { color: #0f172a; }
      </style>
    </head>
    <body>
      <h1>Invoice #123</h1>
      <p>Amount due: $4,500.00</p>
    </body>
  </html>
`, {
  format: "A4",           // "A4" | "Letter" | "Legal" | "Tabloid"
  landscape: false,
  printBackground: true,
  margin: { top: "0.5in", bottom: "0.5in", left: "0.5in", right: "0.5in" },
});

fs.writeFileSync("invoice.pdf", pdf);

Options:

OptionTypeDefaultDescription
formatstring"A4"Paper format
landscapebooleanfalseLandscape orientation
printBackgroundbooleantrueInclude background colors/images
marginobject{ top, right, bottom, left } in CSS units
scalenumber1Scale factor 0.1–2
pageRangesstringPages to print e.g. "1-5, 8"

client.screenshot(options)

Screenshot a URL or render HTML. Returns a Buffer. Provide url OR html, not both.

// From a URL
const img = await client.screenshot({
  url: "https://mysite.com/blog/post",
  width: 1200,
  height: 630,
  format: "png",
});

// From HTML (e.g. OG image)
const img = await client.screenshot({
  html: `<div style="width:1200px;height:630px;background:#0f172a;
                      display:flex;align-items:center;padding:80px">
           <h1 style="color:white;font-size:64px">My Blog Post</h1>
         </div>`,
  width: 1200,
  height: 630,
});

fs.writeFileSync("og-image.png", img);

Options:

OptionTypeDefaultDescription
urlstringURL to screenshot
htmlstringHTML to render
widthnumber1200Viewport width
heightnumber630Viewport height
formatstring"png""png" or "jpeg"

client.credits()

Check remaining credits and USDC top-up address. Agent accounts only.

const { credits, usdc_address } = await client.credits();
console.log(`${credits} credits remaining`);

client.creditsRemaining

After any pdf() or screenshot() call, client.creditsRemaining holds the value from the X-Credits-Remaining response header. Use this to trigger proactive USDC top-ups.

const pdf = await client.pdf(html);
if ((client.creditsRemaining ?? 999) < 50) {
  // top up via USDC
}

DocAPI.register(options?)

Register a new agent account programmatically. No API key required.

const account = await DocAPI.register({
  notify_email: "ops@yourcompany.com", // optional low-balance alerts
});

console.log(account.api_key);       // "pk_..."
console.log(account.usdc_address);  // "0x..."
console.log(account.free_calls);    // 10

Error handling

All errors throw DocAPIError with status and code properties.

import { DocAPI, DocAPIError } from "@docapi/sdk";

try {
  const pdf = await client.pdf("<h1>Hello</h1>");
} catch (err) {
  if (err instanceof DocAPIError) {
    console.error(err.status); // 401, 402, 429, 500
    console.error(err.code);   // "invalid_api_key", "credits_exhausted", etc.
    console.error(err.message);
  }
}
StatusCodeMeaning
401invalid_api_keyInvalid or missing API key
402credits_exhaustedAgent account out of credits — send USDC
429usage_limit_exceededMonthly plan limit reached
500generation_failedRendering error

Next.js example

// app/api/og/route.ts
import { DocAPI } from "@docapi/sdk";
import { NextRequest } from "next/server";

const client = new DocAPI(process.env.DOCAPI_KEY!);

export async function GET(req: NextRequest) {
  const title = req.nextUrl.searchParams.get("title") ?? "My Page";

  const img = await client.screenshot({
    html: `<div style="width:1200px;height:630px;background:#0f172a;
                        display:flex;align-items:center;padding:80px;
                        font-family:system-ui,sans-serif">
             <h1 style="color:white;font-size:64px">${title}</h1>
           </div>`,
    width: 1200,
    height: 630,
  });

  return new Response(img, {
    headers: {
      "Content-Type": "image/png",
      "Cache-Control": "public, max-age=86400",
    },
  });
}

Django example

# Use the REST API directly — Python SDK coming soon
import requests

response = requests.post(
    "https://api.docapi.co/v1/pdf",
    headers={"x-api-key": os.environ["DOCAPI_KEY"]},
    json={"html": html_string, "options": {"format": "A4"}},
)
pdf_bytes = response.content

Keywords

pdf

FAQs

Package last updated on 03 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts