Dynamic generation
Make and update banners from data with no editor: read the fields of a source banner, send new values and wait for the exports.
Dynamic generation makes banners from data. You design a source banner in the editor, usually a template, and give its layers clear names. Your server then sends new values for those names. The API makes a new banner, or it updates an existing one. No editor opens.
Use cases
- Product banners: one banner for each product of a catalog, with its name, price and photo.
- Languages: one banner for each language, from the same design.
- Test variants: several headlines or buttons for an A/B test (a test that compares two versions).
- Personal offers: a banner with the name or the discount of one customer.
- Scheduled updates: a daily change of a countdown or a price in a banner that is already in use, with
edit-banner-with-data. - Data sync: new values from your CRM or stock system when the data changes.
The actions
| Action | What it does |
|---|---|
get-banner-fields |
Lists the layers of a banner that take new values in generate-banner and edit-banner-with-data. |
get-bannerset-fields |
Lists the layers of all banners in a banner set that take new values in generate-bannerset and edit-bannerset-with-data. |
generate-banner |
Makes a new banner from a source banner, with new values for its fields. |
generate-bannerset |
Makes a new banner set from a source banner set, with new values for its fields. |
edit-banner-with-data |
Overwrites the fields of an existing banner with new values. |
edit-bannerset-with-data |
Overwrites the fields of the banners in an existing banner set with new values. |
Step 1: read the fields
Call get-banner-fields with the hash of the source banner, or get-bannerset-fields with the hash of a source banner set. Each field has a type and a name. The name is the layer name from the editor.
{
"code": 200,
"data": [
{
"type": "background",
"name": "MainBackgroundImage"
},
{
"type": "text",
"name": "Headline"
},
{
"type": "image",
"name": "Product photo"
},
{
"type": "button",
"name": "CTA"
}
]
}
| Type | Listed when | Value | Effect |
|---|---|---|---|
text |
A text layer inside a slide. | The new text. | Replaces the text. A text with several styles keeps only the styles that cover the whole new text. |
button |
A button layer inside a slide. | The new button label. | Replaces the button label. |
image |
An image layer inside a slide. | The URL of the new image. | Replaces the image. |
svg |
An SVG layer inside a slide. | The URL of the new SVG file. | Replaces the SVG. |
background |
The background of the banner is an image. The field name is always MainBackgroundImage. | The URL of the new background image. | Replaces the background image. |
- Values match layers by name only. One value changes every layer with that name inside a slide.
- The API ignores names that the banner does not have. Layers without a value keep their content.
- A layer outside the slides, which shows on every slide, is in the list with its own type. The API applies a value to it when it is a text, button, image or SVG layer, and ignores a value for any other type, for example a shape.
get-bannerset-fieldscan list the same name and type more than once. One value fills all of them.- If two layers on one slide have the same name,
generate-bannerandgenerate-bannersetfail with 531. - Send every value as a string, also a number.
Step 2: write the values
Send the values in the data parameter, as a JSON array of
objects with name and value:
[
{
"name": "Headline",
"value": "50% off today"
},
{
"name": "Product photo",
"value": "https://cdn.example.com/p/123.png"
},
{
"name": "MainBackgroundImage",
"value": "https://cdn.example.com/bg.jpg"
}
]
- Send every value as a string, also a number.
-
URL-encode
datain the query string, and sign it like every other parameter. -
To change only
banner_urlanduse_as_clicktag, send data=[]. -
The whole request URL,
dataincluded, must fit the limit of the web server, which is about 8 KB by default. This limit was not tested. Send the URL of an image, not the image.
Send banner_url with every edit
edit-banner-with-data and edit-bannerset-with-data set the click-through URL and the click tag from the request. If you leave out banner_url, the banner loses its click-through URL, and its click tag turns off. Send banner_url and use_as_clicktag again with each edit.
For generate-banner and generate-bannerset, a missing banner_url gives the new banner an empty click-through URL. The new banner does not get the URL of the source.
Step 3: generate the banner
Call generate-banner with identifier, from_banner, banner_name and data. The new banner belongs to the user with this identifier. The source can belong to any user of your white label.
This script sends one generate-banner request. It reads
the secret key from the environment variable
BANNERSNACK_SECRET_KEY. Replace YOUR_CLIENT_ID
with your client id.
import { createHmac } from 'node:crypto';
const params = {
"action": "generate-banner",
"banner_name": "Summer sale - Berlin",
"banner_url": "https://www.example.com/summer-sale",
"client_id": "YOUR_CLIENT_ID",
"data": "[{\"name\":\"Headline\",\"value\":\"50% off today\"},{\"name\":\"Product photo\",\"value\":\"https://cdn.example.com/p/123.png\"}]",
"from_banner": "bduny9j1",
"identifier": "user-42",
"use_as_clicktag": "true"
};
params.t = String(Math.floor(Date.now() / 1000));
const data = Object.keys(params).sort().map(key => key + params[key]).join('');
params.signature = createHmac('sha1', process.env.BANNERSNACK_SECRET_KEY).update(data).digest('hex');
const url = "https://enterprise.bannersnack.com/wapi/2.0/" + '?' + new URLSearchParams(params);
const response = await fetch(url);
console.log(await response.json());
The answer has the hash of the new banner:
{
"code": 200,
"data": {
"hash": "bk3new8pz"
}
}
Step 4: wait for the exports
The API starts the image and HTML5 exports of the new banner at once. Call get-banner-urls until png and html5 are done. Then show the png image, or use the HTML5 zip. For a banner set, call get-bannerset-urls.
The Exports guide shows how to
wait.
Update a banner that is in use
edit-banner-with-data changes a banner in place, and its hash stays the same. The API has no action that undoes this. The edit also resets the exports of the banner. The image and HTML5 exports start again, and the other types go back to none until you start them again.
edit-bannerset-with-data changes the banners of a banner set in the same way.
Generate a batch
The API makes one banner for each call. To make a batch, call generate-banner once for each row of your data. Give each banner its own name, for example from the name of the source and the row.
Wait between calls, for example 1.5 seconds, and stop at the first
error. The generation actions do not check the banner limit of your
white label, so they never answer 517. In this example,
callApi is your function that signs a request and sends
it.
const rows = [
{ city: 'Berlin', headline: 'Summer sale in Berlin', photo: 'https://cdn.example.com/berlin.png' },
{ city: 'Paris', headline: 'Summer sale in Paris', photo: 'https://cdn.example.com/paris.png' }
];
for (const [index, item] of rows.entries()) {
const answer = await callApi('generate-banner', {
identifier: 'user-42',
from_banner: 'bduny9j1',
banner_name: 'Summer sale - ' + item.city,
banner_url: 'https://www.example.com/summer-sale',
use_as_clicktag: 'true',
data: JSON.stringify([
{ name: 'Headline', value: item.headline },
{ name: 'Product photo', value: item.photo }
])
});
if (answer.code !== 200) {
throw new Error('Row ' + (index + 1) + ' failed: ' + JSON.stringify(answer));
}
await new Promise(resolve => setTimeout(resolve, 1500));
}
To try a batch without code, open the generate-banner or generate-bannerset page. Its try-it panel loads the fields of your template and runs one call for each row of CSV or JSON data that you paste. It waits 1.5 seconds between calls, stops at the first error, and takes at most 200 rows.
Errors
For most failures, the generation actions answer with the code and the text swapped:
{
"type": "error",
"code": "Banner not found",
"data": "531"
}
- The source does not exist, is not in your white label, or is the wrong kind of item.
- Two layers on one slide of the source have the same name. Give each layer on a slide its own name.
- The save of the new banner failed.
If data is missing, is not valid JSON, or has an item without a name or a value, the answer is {"type":"error","code":521,"data":"Internal server error"}. A new identifier with a character that is not allowed gives 541. Such a display name in name gives 540.
Known limits
- A new text keeps only the text styles that cover the whole new text. Give a text layer one style if you replace its text.
-
get-banner-fieldsdoes not report two layers with the same name on one slide.
History of the feature
The older documentation does not describe dynamic generation. These are the changes to it, by the date of the change. All of them reached the main version of the API in October 2020. The behaviour of the feature did not change after that.
| Date | Change |
|---|---|
| 2020-07-28 | The API gets get-banner-fields, get-bannerset-fields, generate-banner, generate-bannerset, edit-banner-with-data and edit-bannerset-with-data. Values replace text, image and button layers. |
| 2020-07-30 | The background image becomes the field MainBackgroundImage. SVG layers become fields. New parameters banner_url and use_as_clicktag. Two layers with the same name on one slide make generate-banner fail. |
| 2020-08-27 | generate-banner starts the HTML5 export of the new banner. |
| 2020-10-22 | A new text keeps only the text styles that cover the whole new text. |