Get start
Plai is the ad tool built for small teams & creators to launch ads like a pro, no experience required. Simply plug in your creative and budget, and Plai runs your ad campaign for you.
To use this API, you need an API key. Please find "Developer Center" by clicking your name appearing in the top right corner of the dashboard. You can generate your API key by clicking "Generate API Key" button.
Sample Code
import axios from "axios";
import {
SampleCodeBingCreativeTemplate,
SampleCodeFacebookCreativeTemplate,
SampleCodeGoogleDisplayCreativeTemplate,
SampleCodeGooglePerformanceMaxCreativeTemplate,
SampleCodeGoogleSearchCreativeTemplate,
SampleCodeLinkedInCreativeTemplate,
SampleCodeSnapCreativeTemplate,
SampleCodeSpotifyCreativeTemplate,
SampleCodeTikTokCreativeTemplate,
SampleCodeYoutubeCreativeTemplate,
SamplePublicBrandContextRequest,
} from "./sample_code_schema";
// Please set your API key here.
const HEADER = {
headers: { "X-API-Key": "" },
};
// Please see detail in https://partner.plai.io/api-docs/
const API_DOMAIN = "https://partner.plai.io";
// This method gets id of your workspace.
// WorkspaceId is needed to make most of the API calls.
const getWorkspaceId = async () => {
const res = await axios.get(`${API_DOMAIN}/public/workspace`, HEADER);
let workspaceId = "";
res.data.workspaces.forEach((workspace: any) => {
workspaceId = workspace.id;
});
return workspaceId;
};
// This method lists all creative templates in your workspace.
const listCreativeTemplate = async (workspaceId: string) => {
const res = await axios.get(
`${API_DOMAIN}/public/workspace/${workspaceId}/creative_template`,
HEADER
);
const creativeTemplates = res.data.creative_templates;
return creativeTemplates;
};
// This method gets a creative template by its id.
const getCreativeTemplate = async (
workspaceId: string,
creativeTemplateId: string
) => {
const res = await axios.get(
`${API_DOMAIN}/public/workspace/${workspaceId}/creative_template/${creativeTemplateId}`,
HEADER
);
const creativeTemplate = res.data.creative_template;
return creativeTemplate;
};
// This method creates a Google Search creative template.
const createGoogleSearchCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeGoogleSearchCreativeTemplate = {
website: "https://www.plai.io/",
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
platform: "GOOGLE_SEARCH",
name: `My Google Search Creative Template - ${Date.now()}`,
workspaceId,
//
siteLink: [
{
sitelinkText: "SiteLink",
line1: "Recommended1",
line2: "Recommended2",
url: "http://a1.com",
},
],
//
callExtension: {
countryCode: "US",
phoneNumber: "1234567890",
},
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Google Search creative template with SiteLink.
const createGoogleSearchCreativeTemplateWithSiteLink = async (
workspaceId: string
) => {
const requestBase: SampleCodeGoogleSearchCreativeTemplate = {
website: "https://www.plai.io/",
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
platform: "GOOGLE_SEARCH",
name: `My Google Search Creative Template with SiteLink - ${Date.now()}`,
workspaceId,
siteLink: [
{
sitelinkText: "SiteLink",
line1: "Recommended1",
line2: "Recommended2",
url: "http://a1.com",
},
],
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Google Search creative template with Call Extension.
const createGoogleSearchCreativeTemplateWithCallExtension = async (
workspaceId: string
) => {
const requestBase: SampleCodeGoogleSearchCreativeTemplate = {
website: "https://www.plai.io/",
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
platform: "GOOGLE_SEARCH",
name: `My Google Search Creative Template with Call Extension - ${Date.now()}`,
workspaceId,
callExtension: {
countryCode: "US",
phoneNumber: "1234567890",
},
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Facebook creative template with image media.
const createFacebookImageCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeFacebookCreativeTemplate = {
website: "https://www.plai.io/",
mediaType: "IMAGE",
callToAction: "SUBSCRIBE",
primaryTexts: ["primary text 1", "primary text 2"],
headlines: ["headline 1", "headline 2"],
squareImages: [
"https://test.com/square1.jpg",
"https://test.com/square2.jpg",
],
rectangleImages: [
"https://test.com/rectangle1.jpg",
"https://test.com/rectangle2.jpg",
],
verticalImages: [
"https://test.com/vertical1.jpg",
"https://test.com/vertical2.jpg",
],
platform: "FACEBOOK",
name: `My Facebook Call Image Creative Template - ${Date.now()}`,
workspaceId,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Facebook creative template with video media.
const createFacebookVideoCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeFacebookCreativeTemplate = {
website: "https://www.plai.io/",
mediaType: "VIDEO",
callToAction: "SUBSCRIBE",
primaryTexts: ["primary text 1", "primary text 2"],
headlines: ["headline 1", "headline 2"],
videos: [
{
thumbnailUrl: "https://test.com/thumbnail1.jpg",
videoUrl: "https://test.com/video1.mp4",
},
],
platform: "FACEBOOK",
name: `My Facebook Video Creative Template - ${Date.now()}`,
workspaceId,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Facebook creative template with image media.
const createFacebookCallAdsCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeFacebookCreativeTemplate = {
website: "https://www.plai.io/",
mediaType: "IMAGE",
callToAction: "SUBSCRIBE",
primaryTexts: ["primary text 1", "primary text 2"],
headlines: ["headline 1", "headline 2"],
squareImages: [
"https://test.com/square1.jpg",
"https://test.com/square2.jpg",
],
rectangleImages: [
"https://test.com/rectangle1.jpg",
"https://test.com/rectangle2.jpg",
],
verticalImages: [
"https://test.com/vertical1.jpg",
"https://test.com/vertical2.jpg",
],
platform: "FACEBOOK",
name: `My Facebook Call Image Creative Template - ${Date.now()}`,
workspaceId,
phoneNumber: "14343434344",
isCallAds: true,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createFacebookMessengerCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeFacebookCreativeTemplate = {
website: "https://www.plai.io/",
mediaType: "IMAGE",
callToAction: "SUBSCRIBE",
primaryTexts: ["primary text 1", "primary text 2"],
headlines: ["headline 1", "headline 2"],
squareImages: [
"https://test.com/square1.jpg",
"https://test.com/square2.jpg",
],
rectangleImages: [
"https://test.com/rectangle1.jpg",
"https://test.com/rectangle2.jpg",
],
verticalImages: [
"https://test.com/vertical1.jpg",
"https://test.com/vertical2.jpg",
],
platform: "FACEBOOK",
name: `My Facebook Messenger Creative Template - ${Date.now()}`,
workspaceId,
isMessenger: true,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createBingCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeBingCreativeTemplate = {
website: "https://www.plai.io/",
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
platform: "BING_ADS",
name: `My Bing Creative Template - ${Date.now()}`,
workspaceId,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Facebook creative template with image media.
const createInstagramImageCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeFacebookCreativeTemplate = {
website: "https://www.plai.io/",
mediaType: "IMAGE",
callToAction: "SUBSCRIBE",
primaryTexts: ["primary text 1", "primary text 2"],
headlines: ["headline 1", "headline 2"],
squareImages: [
"https://test.com/square1.jpg",
"https://test.com/square2.jpg",
],
rectangleImages: [
"https://test.com/rectangle1.jpg",
"https://test.com/rectangle2.jpg",
],
verticalImages: [
"https://test.com/vertical1.jpg",
"https://test.com/vertical2.jpg",
],
platform: "INSTAGRAM",
name: `My Instagram Call Image Creative Template - ${Date.now()}`,
workspaceId,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
// This method creates a Facebook creative template with video media.
const createInstagramVideoCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeFacebookCreativeTemplate = {
website: "https://www.plai.io/",
mediaType: "VIDEO",
callToAction: "SUBSCRIBE",
primaryTexts: ["primary text 1", "primary text 2"],
headlines: ["headline 1", "headline 2"],
videos: [
{
thumbnailUrl: "https://test.com/thumbnail1.jpg",
videoUrl: "https://test.com/video1.mp4",
},
],
platform: "INSTAGRAM",
name: `My Instagram Video Creative Template - ${Date.now()}`,
workspaceId,
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createYoutubeDiscoveryAdCreativeTemplate = async (
workspaceId: string
) => {
const requestBase: SampleCodeYoutubeCreativeTemplate = {
website: "https://www.plai.io/",
platform: "YOUTUBE",
name: `Youtube Creative Template - ${Date.now()}`,
workspaceId,
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
videoIds: ["v44fDPzr1GI"],
youtubeCreativeType: "DISCOVERY_ADS",
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createYoutubeInStreamAdCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeYoutubeCreativeTemplate = {
website: "https://www.plai.io/",
platform: "YOUTUBE",
name: `Youtube In-Stream Ad Creative Template - ${Date.now()}`,
workspaceId,
videoIds: ["v44fDPzr1GI"],
youtubeCreativeType: "IN_STREAM_ADS",
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createGoogleDisplayCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeGoogleDisplayCreativeTemplate = {
website: "https://www.plai.io/",
platform: "GOOGLE_DISPLAY",
name: `My Google Display Creative Template - ${Date.now()}`,
workspaceId,
businessName: "plai",
squareImages: [
"https://test.com/square1.jpg",
"https://test.com/square2.jpg",
],
rectangleImages: [
"https://test.com/rectangle1.jpg",
"https://test.com/rectangle2.jpg",
],
verticalImages: [
"https://test.com/vertical1.jpg",
"https://test.com/vertical2.jpg",
],
logoImages: ["https://test.com/logo1.jpg", "https://test.com/logo2.jpg"],
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createGooglePerformanceMaxCreativeTemplate = async (
workspaceId: string
) => {
const requestBase: SampleCodeGooglePerformanceMaxCreativeTemplate = {
website: "https://www.plai.io/",
platform: "GOOGLE_PMAX",
name: `My Google Performance Max Creative Template - ${Date.now()}`,
workspaceId,
businessName: "plai",
squareImages: [
"https://test.com/square1.jpg",
"https://test.com/square2.jpg",
],
rectangleImages: [
"https://test.com/rectangle1.jpg",
"https://test.com/rectangle2.jpg",
],
verticalImages: [
"https://test.com/vertical1.jpg",
"https://test.com/vertical2.jpg",
],
logoImages: ["https://test.com/logo1.jpg", "https://test.com/logo2.jpg"],
headlines: ["headline1", "headline2", "headline3", "headline4"],
descriptions: ["description1", "description2"],
longHeadlines: ["long headline 1", "long headline 2"],
videoIds: ["v44fDPzr1GI"],
callExtension: {
countryCode: "US",
phoneNumber: "1234567890",
},
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createTikTokCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeTikTokCreativeTemplate = {
website: "https://www.plai.io/",
platform: "TIKTOK",
name: `My TikTok Creative Template - ${Date.now()}`,
workspaceId,
descriptions: ["description1", "description2"],
businessName: "plai",
tiktokVideos: ["https://test.com/test.mp4"],
logoImages: ["https://test.com/test.jpg"],
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createSnapCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeSnapCreativeTemplate = {
website: "https://www.plai.io/",
platform: "SNAPCHAT",
name: `My Snap Creative Template - ${Date.now()}`,
workspaceId,
businessName: "plai",
descriptions: ["description1", "description2"],
verticalImages: ["https://test.com/test.jpg"],
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createSpotifyCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeSpotifyCreativeTemplate = {
website: "https://www.plai.io/",
platform: "SPOTIFY",
name: `My Spotify Creative Template - ${Date.now()}`,
workspaceId,
businessName: "plai",
squareImages: ["https://test.com/test_square.jpg"],
logoImages: ["https://test.com/test_logo.jpg"],
spotifyAudios: ["https://test.com/test.mp3"],
callToAction: "LEARN_MORE",
callToActionLanguage: "ENGLISH",
headlines: ["headline1", "headline2", "headline3", "headline4"],
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createLinkedInImageCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeLinkedInCreativeTemplate = {
website: "https://www.plai.io/",
platform: "LINKEDIN",
name: `My LinkedIn Creative Template - ${Date.now()}`,
workspaceId,
mediaType: "IMAGE",
squareImages: ["https://test.com/test_square.jpg"],
headlines: ["headline1", "headline2", "headline3", "headline4"],
primaryTexts: ["primary text 1", "primary text 2"],
callToAction: "DOWNLOAD",
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createLinkedInVideoCreativeTemplate = async (workspaceId: string) => {
const requestBase: SampleCodeLinkedInCreativeTemplate = {
website: "https://www.plai.io/",
platform: "LINKEDIN",
name: `My LinkedIn Video Creative Template - ${Date.now()}`,
workspaceId,
mediaType: "VIDEO",
headlines: ["headline1", "headline2", "headline3", "headline4"],
primaryTexts: ["primary text 1", "primary text 2"],
callToAction: "DOWNLOAD",
videos: [
{
thumbnailUrl: "https://test.com/test_thumbnail.jpg",
videoUrl: "https://test.com/test_video.mp4",
},
],
};
const res = await axios.post(
`${API_DOMAIN}/public/creative_template`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const createBrandContext = async (workspaceId: string) => {
const requestBase: SamplePublicBrandContextRequest = {
workspaceId,
url: "https://test.com/",
brandName: "Test",
industry: "Test industry",
languageCode: "en",
phoneNumber: "1111111",
font: "OPEN_SANS",
brandColor: "216,15,49",
secondaryColor: "76,20,248",
brandVoice: "Soft and formal",
logoImageUrl: "https://test.com/61.webp",
};
const res = await axios.post(
`${API_DOMAIN}/public/brand_context`,
requestBase,
HEADER
);
console.log(res.status);
console.log(res.data);
};
const getBrandContext = async (workspaceId: string) => {
const res = await axios.get(
`${API_DOMAIN}/public/workspace/${workspaceId}/brand_context`,
HEADER
);
console.log(res.status);
console.log(res.data);
};
(async () => {
console.log("start getting workspace id");
const workspaceId = await getWorkspaceId();
console.log("workspace id: ", workspaceId);
console.log("start getting creative templates");
const creativeTemplates = await listCreativeTemplate(workspaceId);
console.log("creative templates: ", creativeTemplates);
creativeTemplates.forEach(async (creativeTemplate: any) => {
const name = creativeTemplate.name;
console.log(`Got creative template: ${name}`);
const ct = await getCreativeTemplate(workspaceId, creativeTemplate.id);
console.log(`Showing creative template: ${name}`);
console.log(JSON.stringify(ct, null, 2));
});
// To create a new creative template, uncomment the following lines and run the script again.
// This functions have dummy data. Pleae update with your own data.
// await createGoogleSearchCreativeTemplate(workspaceId);
// await createFacebookImageCreativeTemplate(workspaceId);
// await createFacebookVideoCreativeTemplate(workspaceId);
// await createFacebookCallAdsCreativeTemplate(workspaceId);
// await createFacebookMessengerCreativeTemplate(workspaceId);
// await createBingCreativeTemplate(workspaceId);
// await createInstagramImageCreativeTemplate(workspaceId);
// await createInstagramVideoCreativeTemplate(workspaceId);
// await createYoutubeDiscoveryAdCreativeTemplate(workspaceId);
// await createYoutubeInStreamAdCreativeTemplate(workspaceId);
// await createGoogleDisplayCreativeTemplate(workspaceId);
// await createGooglePerformanceMaxCreativeTemplate(workspaceId);
// await createTikTokCreativeTemplate(workspaceId);
// await createSnapCreativeTemplate(workspaceId);
// await createSpotifyCreativeTemplate(workspaceId);
// await createLinkedInImageCreativeTemplate(workspaceId);
// await createLinkedInVideoCreativeTemplate(workspaceId);
// await createBrandContext(workspaceId);
// await getBrandContext(workspaceId);
})();