17 added
18 removed
Original
2026-01-01
Modified
2026-03-10
1
<p><em>Solution Recipes are tutorials to achieve specific objectives in Klaviyo. They can also help you master Klaviyo, learn new third-party technologies, and come up with creative ideas. They are written mainly for developers and technically-advanced users.</em></p>
1
<p><em>Solution Recipes are tutorials to achieve specific objectives in Klaviyo. They can also help you master Klaviyo, learn new third-party technologies, and come up with creative ideas. They are written mainly for developers and technically-advanced users.</em></p>
2
-
<p><strong><em>Note:</em></strong><em>We do our best to make sure any code and API references are accurate and current when this is published, but you might need to update code and it’s always a best practice to leverage our</em><a><em>latest API versions</em></a><em>. If you have questions, feel free to hop over to our</em><a><em>Developer Community</em></a><em>.</em></p>
2
+
<p><em><strong>Note:</strong>We do our best to make sure any code and API references are accurate and current when this is published, but you might need to update code and it’s always a best practice to leverage our<a>latest API versions</a>. If you have questions, feel free to hop over to our<a>Developer Community</a>.</em></p>
3
<p>What you’ll learn</p>
3
<p>What you’ll learn</p>
4
<p>Integrate with a third-party API such as OpenAI to generate dynamic content and import it into your Klaviyo account, to use in campaigns and messages.</p>
4
<p>Integrate with a third-party API such as OpenAI to generate dynamic content and import it into your Klaviyo account, to use in campaigns and messages.</p>
5
<p>Level of sophistication</p>
5
<p>Level of sophistication</p>
6
<p>Moderate</p>
6
<p>Moderate</p>
7
<h2>Introduction</h2>
7
<h2>Introduction</h2>
8
-
<p>This solution recipe will show you how to create awesome AI-generated images for your next Klaviyo campaign. </p>
8
+
<p>This solution recipe will show you how to create awesome AI-generated images for your next Klaviyo campaign.</p>
9
<p>In this case, we’re a brand that sells avocados and avocado accessories, and we’re looking to create the perfect illustration for a big sale campaign.</p>
9
<p>In this case, we’re a brand that sells avocados and avocado accessories, and we’re looking to create the perfect illustration for a big sale campaign.</p>
10
<h2>Challenge</h2>
10
<h2>Challenge</h2>
11
<p>Give AI a prompt for creating the perfect image for my brand’s upcoming email campaign campaign, get that into my Klaviyo Images & Brand library, and create an email campaign using the asset.</p>
11
<p>Give AI a prompt for creating the perfect image for my brand’s upcoming email campaign campaign, get that into my Klaviyo Images & Brand library, and create an email campaign using the asset.</p>
12
<p>Klaviyo recently released<a>APIs</a>for uploading images to your account, so this is now possible!</p>
12
<p>Klaviyo recently released<a>APIs</a>for uploading images to your account, so this is now possible!</p>
13
<p><em>Note that you should check any AI-created images for brand compliance and adhere to any copyright or monetization concerns. Before you begin, we recommend consulting your legal team before using any AI service to ensure you’re in compliance with your company’s policies. It is also your responsibility to ensure that your usage of any images that OpenAI generates and which you use in Klaviyo campaigns meets the<a>OpenAI usage policy</a>.</em></p>
13
<p><em>Note that you should check any AI-created images for brand compliance and adhere to any copyright or monetization concerns. Before you begin, we recommend consulting your legal team before using any AI service to ensure you’re in compliance with your company’s policies. It is also your responsibility to ensure that your usage of any images that OpenAI generates and which you use in Klaviyo campaigns meets the<a>OpenAI usage policy</a>.</em></p>
14
<h2>Preparation</h2>
14
<h2>Preparation</h2>
15
-
<h3>Get started with Klaviyo’s APIs </h3>
15
+
<h3>Get started with Klaviyo’s APIs</h3>
16
-
<ol><li><a>Create a test account</a>if you don’t have one already for experimentation purposes. </li>
16
+
<ol><li><a>Create a test account</a>if you don’t have one already for experimentation purposes.</li>
17
-
<li>Fork our latest Postman Collection from the<a>Klaviyo Developers workspace</a>, or<a>fork the 2023-10-15 collection directly</a>. </li>
17
+
<li>Fork our latest Postman Collection from the<a>Klaviyo Developers workspace</a>, or<a>fork the 2023-10-15 collection directly</a>.</li>
18
<li>Create and retrieve<a>your private API key</a>.</li>
18
<li>Create and retrieve<a>your private API key</a>.</li>
19
</ol><h3>Get started with OpenAI’s Dall-E API</h3>
19
</ol><h3>Get started with OpenAI’s Dall-E API</h3>
20
-
<ol><li><a>Create an OpenAI account</a>if you don’t already have one! </li>
20
+
<ol><li><a>Create an OpenAI account</a>if you don’t already have one!</li>
21
<li>They have a<a>great API overview</a>and a<a>quickstart Python SDK guide</a>.</li>
21
<li>They have a<a>great API overview</a>and a<a>quickstart Python SDK guide</a>.</li>
22
-
<li>Create and retrieve<a>your OpenAI API key</a>. </li>
22
+
<li>Create and retrieve<a>your OpenAI API key</a>.</li>
23
-
<li>Read OpenAI’s<a>terms and policies</a>. </li>
23
+
<li>Read OpenAI’s<a>terms and policies</a>.</li>
24
<li>To learn more about how OpenAI prices their Dall-E APIs, see their<a>pricing page</a>.</li>
24
<li>To learn more about how OpenAI prices their Dall-E APIs, see their<a>pricing page</a>.</li>
25
</ol><h3>Get started with Napkin.io</h3>
25
</ol><h3>Get started with Napkin.io</h3>
26
<p>If you don’t have one already, sign up for a free<a>Napkin.io</a>account. Napkin is a Klaviyo tool that allows you to write and run server-less functions. You can also use the code-hosting platform of your choice to run the code you will write in this recipe.</p>
26
<p>If you don’t have one already, sign up for a free<a>Napkin.io</a>account. Napkin is a Klaviyo tool that allows you to write and run server-less functions. You can also use the code-hosting platform of your choice to run the code you will write in this recipe.</p>
27
<h2>Using Dall-E APIs to generate images from a prompt</h2>
27
<h2>Using Dall-E APIs to generate images from a prompt</h2>
28
<p>In the following example Postman request, a call is made to OpenAPI’s<a>Create image endpoint</a>:</p>
28
<p>In the following example Postman request, a call is made to OpenAPI’s<a>Create image endpoint</a>:</p>
29
-
<p>Here’s the same request using the OpenAI Python SDK: </p>
29
+
<p>Here’s the same request using the OpenAI Python SDK:</p>
30
import os import openai def openai_generate_images(request): openai.api_key = os.getenv("OPENAI_API_KEY") input = request.body or {} openai_response = openai.Image.create( prompt=input.get("prompt") or "an avocado, in kawaii art style", n=input.get("count") or 1, size=input.get("size") or "1024x1024" ) return openai_response<p>Note that if you set n to a value greater than 1, you will get an image URL for each variant in the data array.</p>
30
import os import openai def openai_generate_images(request): openai.api_key = os.getenv("OPENAI_API_KEY") input = request.body or {} openai_response = openai.Image.create( prompt=input.get("prompt") or "an avocado, in kawaii art style", n=input.get("count") or 1, size=input.get("size") or "1024x1024" ) return openai_response<p>Note that if you set n to a value greater than 1, you will get an image URL for each variant in the data array.</p>
31
<p>Calling the OpenAI API will return a response like the following. Note that the URLs returned by this API expire after an hour, so you’ll want to upload it to Klaviyo before this expiration.</p>
31
<p>Calling the OpenAI API will return a response like the following. Note that the URLs returned by this API expire after an hour, so you’ll want to upload it to Klaviyo before this expiration.</p>
32
-
{ "created": 1698068491, "data": [ { "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-aEQ28uSWRwi3n5i3LjFnBc10/user-yDW3f5wi900b8BH1XHKzdt6v/img-CeSj5ECGNfBafd0RrKKGVkpR.png?st=2023-11-30T00%3A40%3A29Z&se=2023-11-30T02%3A40%3A29Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-11-29T19%3A10%3A01Z&ske=2023-11-30T19%3A10%3A01Z&sks=b&skv=2021-08-06&sig=vX%2BemIRMP/57eI/HAPVEjMMPnJ5FKfetCqeEKlfIC/Y%3D" } ] }<h2>Using Klaviyo’s APIs to upload images </h2>
32
+
{ "created": 1698068491, "data": [ { "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-aEQ28uSWRwi3n5i3LjFnBc10/user-yDW3f5wi900b8BH1XHKzdt6v/img-CeSj5ECGNfBafd0RrKKGVkpR.png?st=2023-11-30T00%3A40%3A29Z&se=2023-11-30T02%3A40%3A29Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-11-29T19%3A10%3A01Z&ske=2023-11-30T19%3A10%3A01Z&sks=b&skv=2021-08-06&sig=vX%2BemIRMP/57eI/HAPVEjMMPnJ5FKfetCqeEKlfIC/Y%3D" } ] }<p>Once you have created an image URL from OpenAI’s Dall-E API, you can upload that as an image asset to your Klaviyo account. Here’s what that looks like:</p>
33
-
<p>Once you have created an image URL from OpenAI’s Dall-E API, you can upload that as an image asset to your Klaviyo account. Here’s what that looks like: </p>
34
<p>Here is an example Python function using<a>Klaviyo’s Python SDK</a>:</p>
33
<p>Here is an example Python function using<a>Klaviyo’s Python SDK</a>:</p>
35
import os from klaviyo_api import KlaviyoAPI def klaviyo_create_image(image_src, image_name): klaviyo = KlaviyoAPI(os.getenv("KLAVIYO_API_KEY"), max_delay=60, max_retries=3, test_host=None) klaviyo_response = klaviyo.Images.upload_image_from_url({ "data": { "type": "image", "attributes": { "import_from_url": image_url, "name": image_name, "hidden": False } } }) return klaviyo_response<h2>Putting the Dall-E and Klaviyo APIs together</h2>
34
import os from klaviyo_api import KlaviyoAPI def klaviyo_create_image(image_src, image_name): klaviyo = KlaviyoAPI(os.getenv("KLAVIYO_API_KEY"), max_delay=60, max_retries=3, test_host=None) klaviyo_response = klaviyo.Images.upload_image_from_url({ "data": { "type": "image", "attributes": { "import_from_url": image_url, "name": image_name, "hidden": False } } }) return klaviyo_response<h2>Putting the Dall-E and Klaviyo APIs together</h2>
36
-
<p>Here’s a<a>Napkin function</a>that uses Dall-E and Klaviyo APIs together. Click<strong>Add to your Functions</strong>to clone the function to your account. </p>
35
+
<p>Here’s a<a>Napkin function</a>that uses Dall-E and Klaviyo APIs together. Click<strong>Add to your Functions</strong>to clone the function to your account.</p>
37
<p>When you clone the function, you should see the code appear in your Napkin account, and any necessary dependencies will also be installed.</p>
36
<p>When you clone the function, you should see the code appear in your Napkin account, and any necessary dependencies will also be installed.</p>
38
-
<p>We now need to configure some environment variables. These details are required so that the function knows which OpenAI account to use for generating images and which Klaviyo account to send the images to. </p>
37
+
<p>We now need to configure some environment variables. These details are required so that the function knows which OpenAI account to use for generating images and which Klaviyo account to send the images to.</p>
39
<p>To add them, navigate to the<strong>Other</strong>tab. Next scroll down to<em>Environment Variables</em>.</p>
38
<p>To add them, navigate to the<strong>Other</strong>tab. Next scroll down to<em>Environment Variables</em>.</p>
40
-
<p>You will need to add the following variables in order to use the Napkin function: </p>
39
+
<p>You will need to add the following variables in order to use the Napkin function:</p>
41
-
<ol><li><strong>KLAVIYO_API_KEY</strong>: This is the<a>Private API Key for your Klaviyo account</a>. </li>
40
+
<ol><li><strong>KLAVIYO_API_KEY</strong>: This is the<a>Private API Key for your Klaviyo account</a>.</li>
42
-
<li><strong>OPENAI_API_KEY</strong>: This is the<a>API key for your OpenAI account</a>. </li>
41
+
<li><strong>OPENAI_API_KEY</strong>: This is the<a>API key for your OpenAI account</a>.</li>
43
-
</ol><p>Then, you can run the function using the default variables included in the function. Or, you can make a POST request to the Napkin URL with a payload like the one below, replacing the prompt, size, and number of variants as you wish. </p>
42
+
</ol><p>Then, you can run the function using the default variables included in the function. Or, you can make a POST request to the Napkin URL with a payload like the one below, replacing the prompt, size, and number of variants as you wish.</p>
44
{ "prompt": "an avocado, in kawaii art style", "count": 1, "size": "1024x1024", "name": "cute avocado photo" }<h2>Building your campaign with your AI-generated image</h2>
43
{ "prompt": "an avocado, in kawaii art style", "count": 1, "size": "1024x1024", "name": "cute avocado photo" }<h2>Building your campaign with your AI-generated image</h2>
45
-
<p>Now that you’ve created an image for your cute avocado image using AI, it’s time to actually use it in a campaign. Create a campaign within your Klaviyo account, add an SMS message (or email), and drop in your avocado image (or whatever image) ready to go. </p>
44
+
<p>Now that you’ve created an image for your cute avocado image using AI, it’s time to actually use it in a campaign. Create a campaign within your Klaviyo account, add an SMS message (or email), and drop in your avocado image (or whatever image) ready to go.</p>
46
<p>Navigate to<strong>Campaigns</strong>, click<strong>Create Campaign,</strong>and provide campaign details like the<em>Avocado Email Campaign</em>below:</p>
45
<p>Navigate to<strong>Campaigns</strong>, click<strong>Create Campaign,</strong>and provide campaign details like the<em>Avocado Email Campaign</em>below:</p>
47
<p>Choose a template.</p>
46
<p>Choose a template.</p>
48
<p>Add your image!</p>
47
<p>Add your image!</p>
49
<p>Profit!</p>
48
<p>Profit!</p>
50
49