Javascript
JavaScript SDK
Learn how to generate images and PDFs with the bannerify-js package.
bannerify-js is the Bannerify client for JavaScript and TypeScript. It has types for every option and returns errors as values, so a failed request does not throw.
Install
Examples
Open only what you need
npm
npm install bannerify-jspnpm
pnpm add bannerify-jsyarn
yarn add bannerify-jsbun
bun add bannerify-jsCreate a client
You need an API key. Create one on the API Keys page of the dashboard and keep it on the server.
import { Bannerify } from "bannerify-js"
const bannerify = new Bannerify(process.env.BANNERIFY_API_KEY!)
createClient(apiKey) returns the same client.
Generate an image
import { writeFile } from "node:fs/promises"
const { result, error } = await bannerify.createImage("tpl_xxx", {
modifications: [{ name: "headline", text: "Summer sale" }],
})
if (error) throw new Error(error.message)
await writeFile("banner.png", Buffer.from(result))
result is an ArrayBuffer that holds the image bytes.
Methods
| Method | Result | Use it to |
|---|---|---|
createImage |
ArrayBuffer |
Get the image bytes |
createPdf |
ArrayBuffer |
Get a PDF |
createStoredImage |
URL string | Get a hosted image URL |
renderVideo (Beta) |
Async iterator of events | Render a video and follow its progress |
renderAndWaitVideo (Beta) |
URL string | Render a video and get its MP4 URL |
createVideo (Beta) |
Video id | Start a video and get its id at once |
getVideo (Beta) |
Video status and URL | Read a video again by its id |
watchVideo (Beta) |
Async iterator of events | Follow a video again by its id |
generateImageSignedUrl |
URL string | Build a link that renders when someone opens it |
Create images and PDFs covers the first three, Render videos covers the video methods, and Signed URL covers the last one.
Handle errors
Every method except generateImageSignedUrl returns an object with either result or error. TypeScript narrows the type after you check error.
const { result, error } = await bannerify.createImage("tpl_xxx")
if (error) throw new Error(`${error.code}: ${error.message}`)
console.log(result.byteLength)
An error has four fields: code, message, docs, and requestId. docs links to the page for that code. Send requestId to support when you need help with a request.
The SDK adds two codes of its own. INVALID_FORMAT means the image format is not png, jpeg, or webp. TIMEOUT_ERROR means the request ran past the timeout. A failure that does not come from the API, such as a lost connection, throws.
generateImageSignedUrl returns a string and throws when the format is not supported.
Options
const bannerify = new Bannerify(process.env.BANNERIFY_API_KEY!, {
timeout: 30_000,
})
| Option | Default | Description |
|---|---|---|
timeout |
60000 |
Request timeout in milliseconds |
baseUrl |
https://api.bannerify.co/v1 |
The API base URL |
fetch |
The global fetch |
A custom fetch, for tests or a proxy |