# Vercel AI SDK → PDF with makesPDF

> For AI agents: a documentation index is available at https://makespdf.com/llms.txt — every docs page also has a markdown twin (append .md to the page URL), including this one: https://makespdf.com/docs/integrations/vercel-ai-sdk.md

The Vercel AI SDK integration is two `tool()` definitions over the same REST
endpoints. Copy them into your app; the
[in-app recipe](https://makespdf.com/docs/integrate) covers the surrounding authoring loop.

## Install

```bash
npm install ai zod
export MAKESPDF_API_KEY=... # mint at https://makespdf.com/settings/api-keys?ref=vercel-ai-sdk
```

AI SDK v5+ names a tool's schema `inputSchema`; v3/v4 releases called it
`parameters`. The definitions below use `inputSchema` and zod's two-argument
`z.record`.

## The two tools

| Tool | Endpoint | Arguments |
| --- | --- | --- |
| `renderMd` | `POST /api/v1/md` | `markdown` |
| `render` | `POST /api/v1/render` | `templateId` or `dsl`, plus `data` |

## Example

```ts
import { tool } from "ai";
import { z } from "zod";

const renderMd = tool({
  description: "Render Markdown to PDF. Returns the file path.",
  inputSchema: z.object({ markdown: z.string() }),
  execute: async ({ markdown }) =>
    renderViaApi("/api/v1/md", {
      markdown, // demo: "# Invoice INV-2026-001\n\nHello **Ada** — total **$7,095.00**.\n"
    }),
});

const render = tool({
  description: "Render a stored templateId or inline DSL to PDF. Returns the file path.",
  inputSchema: z.object({
    templateId: z.string().optional(),
    dsl: z.string().optional(),
    data: z.record(z.string(), z.unknown()).optional(),
  }),
  execute: async (input) => renderViaApi("/api/v1/render", input), // demo: { templateId: "invoice-classic", data: { name: "Ada" } }
});
```

Hand `{ renderMd, render }` to `generateText` or `streamText` as `tools`, the
same as any other AI SDK tool.

## Wiring renderViaApi

`renderViaApi` POSTs Bearer-authed JSON and saves the PDF bytes to a temp
file — the same contract as the python shims: two tools, file-path return,
billing matches the underlying endpoint.

```ts
async function renderViaApi(
  path: "/api/v1/md" | "/api/v1/render",
  body: unknown
): Promise<string> {
  const res = await fetch(`https://makespdf.com${path}`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.MAKESPDF_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(body),
  });
  if (!res.ok) throw new Error(`makesPDF ${res.status}: ${await res.text()}`);

  const file = join(tmpdir(), `makespdf-${crypto.randomUUID()}.pdf`);
  await writeFile(file, Buffer.from(await res.arrayBuffer()));
  return file;
}
```

Node's `fs/promises`, `os.tmpdir()` and `crypto.randomUUID()` are all that
helper needs. On an edge runtime, return the bytes instead of writing a file.

## Demo data

Every framework page here renders the same shared demo values — the python
shims import them from `makespdf.integrations.demo`, and this snippet mirrors
them literally:

```ts
const MARKDOWN_DEMO = "# Invoice INV-2026-001\n\nHello **Ada** — total **$7,095.00**.\n";

const DSL_DEMO =
  'const template = doc({ size: "A4" }, page(col(s("Hello {{name}}"))));\n' +
  'const sampleData = { name: "World" };\n';

const DATA_DEMO = { name: "Ada" };

const TEMPLATE_ID_DEMO = "invoice-classic";
```

## In-app authoring recipe

The [integration recipes](https://makespdf.com/docs/integrate) page carries the fuller
in-app variant of this shim: a live skill fetch with offline fallback,
`validate_dsl` and `render_dsl_preview` tools before the billed render, and the
save-template/render loop. Use it when you are embedding authoring in an app
rather than exposing render tools to an agent.

## Billing and limits

Both tools bill at the underlying endpoint — 1 credit per 10 pages on success,
zero on failure. `/md` caps markdown text at 200KB (inline `data:` images
excluded, 15MB request body); `/render` caps the DSL at 512KB. Rate limit:
10,000 renders/hour per caller.
