Skip to content

Node.js client ​

The official client is the handwriting-ai package. It calls POST /v1/renders and downloads the returned links.

bash
npm install handwriting-ai

The package requires Node.js 18 or newer. It uses the global fetch implementation.

js
import { HandwritingAI, HandwritingAIError } from "handwriting-ai";

const client = new HandwritingAI({
  apiKey: process.env.HANDWRITING_AI_API_KEY,
});

try {
  const result = await client.renders.create({
    text: "Thank you for your order.",
    font_id: "caveat",
    page_size: "a4",
    paper: "college-ruled",
    realism: "natural",
    output: { format: "jpeg", dpi: 150 },
  });

  console.log(result.seed, result.expires_at);
  await result.pages[0].save("note.jpg");
} catch (error) {
  if (error instanceof HandwritingAIError) {
    console.error(error.status, error.code, error.requestId);
  } else {
    throw error;
  }
}

Methods ​

client.renders.create(request, options) sends the render request. options.requestId becomes the X-Request-ID header. options.signal cancels the call. Without a signal, the client stops waiting after 90 seconds.

The returned object uses the same field names as the JSON response. JPEG and PNG results include pages. PDF results include file.

page.download() and file.download() fetch the link and return a Uint8Array. They do not send the API key.

save(filePath) downloads the file and writes it. It is for Node.js. Pass your own path. The client does not choose a filename.

client.status() calls GET /v1/status and does not send the API key.

Errors ​

Failed responses throw HandwritingAIError.

PropertyMeaning
statusHTTP status. 0 means the call did not receive an HTTP response.
codeAPI error code, or network_error, invalid_response, or download_failed.
requestIdResponse request id, when the server sent one.
detailsField errors for validation_error.
retryAfterSeconds to wait after rate_limited.
limit, used, resetAtPage allowance details for quota_exceeded.

download_failed means the render succeeded and the later file download did not. Create the render again if the link has expired.

Another host ​

js
const client = new HandwritingAI({
  apiKey: process.env.HANDWRITING_AI_API_KEY,
  baseUrl: "https://api.handwriting-ai.com",
});

Use baseUrl only when you have been given a different API host. The default is https://api.handwriting-ai.com.

Keys are enabled after you request access.