Javascript
JavaScript SDK
Learn how to generate images and PDFs with the Bannerify JavaScript SDK.
Use bannerify-js to call the Bannerify API from JavaScript and TypeScript without writing raw HTTP requests.
Install
Examples
Open only what you need
npm
npm install bannerify-jspnpm
pnpm add bannerify-jsyarn
yarn add bannerify-jsbun
bun add bannerify-jsInstantiate
You need a project API key before using the SDK. Create one in Dashboard → API Keys and keep it server-side. Afterwards you need to provide it to the client:
import { Bannerify } from "bannerify-js";
const bannerify = new Bannerify('bnfy_xxx');
Response format
The SDK returns errors explicitly so your code can handle failed requests without relying on thrown errors. TypeScript narrows the response after you check the error field.
Every method returns either an error or a result field, never both and never none.
Examples
Open only what you need
Success
{
result: T // the result depends on what method you called
}Error
{
error: {
// A machine readable error code
code: ErrorCode;
// A link to our documentation explaining this error in more detail
docs: string;
// A human readable short explanation
message: string;
// The request id for easy support lookup
requestId: string;
}
}Checking for errors
To check for errors you use the error property, our errors are easy to read and provide a link to our documentation for more information.
import { Bannerify } from "bannerify-js";
const bannerify = new Bannerify('your-api-key');
const { result, error } = await bannerify.generateImageSignedUrl('your-template-id');
if (error) {
// handle potential network or bad request error
// a link to our docs will be in the `error.docs` field
console.error(error.message);
return;
}
// process request
console.log(result);
Options
The constructor accepts some options to customize the behavior:
API key
Your project’s API key
const bannerify = new Bannerify('my-api-key')
Timeout
The timeout for the requests in milliseconds, by default it is 10000ms.
const bannerify = new Bannerify('my-api-key',{
// ...
timeout: 10000
})
Fetch
The fetch client to use, by default it uses the global fetch client.
const bannerify = new Bannerify('my-api-key',{
// ...
fetch: myFetchClient
})