ChatGPT and Marketing Cloud Integration: Real-Time Email Content Generation with ChatGPT API

I couldn’t resist the hype surrounding ChatGPT, so I’ve decided to jump in on the trend and see how it could be used by marketers in Salesforce Marketing Cloud.

ChatGPT claims that some of the common use cases for generative AI in email marketing are:

  1. Personalization: ChatGPT can be trained on customer data, such as purchase history, browsing behavior, and demographic information, to generate personalized messages that resonate with each recipient. Marketers can use this information to create more targeted and relevant email content, such as product recommendations or special offers.
  2. Content generation: ChatGPT can be used to generate content for email campaigns, such as subject lines, email copy, and calls to action. By using a language model like ChatGPT, marketers can save time and resources on content creation while still delivering high-quality messages.
  3. Chatbots: Marketers can use ChatGPT to create chatbots that can interact with customers in real-time through email. Chatbots can be used to answer customer questions, provide product recommendations, and even process orders directly within the email.
  4. A/B testing: ChatGPT can be used to generate multiple variations of email content, which can then be tested against each other to determine which version performs best. By using a language model to generate these variations, marketers can easily create and test multiple versions of email content without the need for additional resources.

I’ve decided to check how much effort it would take to set up a mix of 1 & 2: an automated email that would generate personalized content for each subscriber on the fly, based on an attribute such as the subscriber’s interests – and to my surprise, it turned out to be fairly easy. Think of it as dynamic content with AI-generated text. Let’s get to it!

Get an OpenAI API Key

The only prerequisite you need to fulfill to set up a ChatGPT integration is obtaining an API key from the OpenAI API website:

  1. Go to the OpenAI API website:
  2. Click on the “Get API Key” button
  3. Sign up for an OpenAI account (if you haven’t already)
  4. Choose a plan that best fits your needs (there is a free plan which I have been using for testing purposes)
  5. Follow the instructions to generate an API key

The Chat Completion API endpoint

In order to generate personalized content for each subscriber, we will use the Chat Completion API endpoint. It has many parameters that you can use to fine-tune the responses, but the basic payload you need to send to the API endpoint has two parameters, the AI model to be used and details of the content you’d like it to generate:

Content-Type application/json
Authorization Bearer {{API key}}
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "here you tell ChatGPT what content to generate"}]

My knowledge of AI models is very limited, so I used the default one and skipped the remaining settings, but one thing that you need to keep in mind is the usage of tokens, which are ChatGPT’s “currency”, just like super messages in Marketing Cloud: tokens are essentially access codes that allow you to make API calls to the GPT model. When you sign up for OpenAI’s API service, you will be assigned a certain number of tokens based on the plan you choose. Each API call to the GPT model consumes a certain number of tokens, which are deducted from your account. The number of tokens consumed per API call can vary depending on the length and complexity of the text generated by the model. You can control the usage of tokens by adding an additional parameter to your payload, specifying the maximum number of tokens to be used to generate the chat completion, eg.

"max_tokens": 100

Embed the API call in the email body

Let’s start with a disclaimer – do not try to use this for bulk sends. The way I’ve set it up, each email sent will have unique content, but it also means that each email will make a separate API call to ChatGPT. Therefore, each email sent will take more time than it would to send out a generic email, and it will cost you “tokens”.

You can paste the below SSJS directly into a Marketing Cloud email. Note, that you will need to change the following to make it work:

  • In line 5, link the interest variable to the data/attributes you want to use as the basis for personalizing content
  • In line 9, add your API key
  • In line 11, add your prompt
<script runat = "server" >
Platform.Load("Core", "1");
try {
var interest = "dogs"; //add your interest
var contentType = 'application/json';
var headerNames = ["Authorization"];
var headerValues = ["Bearer xxxx"]; //add your key
var jsonBody = '{"model": "gpt-3.5-turbo","messages": [{"role": "user", "content": "Write a marketing email about ' + interest + '."}],"temperature": 0.5,"max_tokens": 100}'
var requestUrl = ";;
var request = HTTP.Post(requestUrl, contentType, jsonBody, headerNames, headerValues);
var respo = request.Response.toString();
var json = Platform.Function.ParseJSON(respo);
var content = json.choices[0].message.content;
Write(content); //prints the content of the email
} catch (error) {
view raw chatGPT.html hosted with ❤ by GitHub

To see how it works in practice, visit the CloudPage I created here. Once you fill in the form, it will send you an email with AI-generated content based on your interest.


While the technical aspects of using ChatGTP API to dynamically generate email content are relatively simple, the true difficulty lies in crafting effective prompts that lead to meaningful output and understanding the various AI models and settings available for use in API calls. Other factors to take into account include performance, cost, and legal considerations. In conclusion, I think it’s best to wait until Salesforce’s native generative AI, Einstein GPT, becomes available for use with Marketing Cloud.

Questions? Comments?

Leave a comment below or email me at

6 thoughts on “ChatGPT and Marketing Cloud Integration: Real-Time Email Content Generation with ChatGPT API

  1. Leny Madalena

    My name is Leny, I work with MC for quite some time and I follow you page etc..
    Thank you so much for this content. Really great.
    However, did you really manage to make it work inside email preview? If so, may I ask how? Cause I am doing that, and in the cloud pages, it works well, but in the email, it doesn’t. It shows the message Unable to generate preview.

    Do you know any specific reason to not work inside email? At least in my case?

    Thank you



    1. Hi Leny, yes, it works in an email just fine – I am not sure why it would fail, but I did notice that if you’re on the free ChatGPT plan, it’s not always 100% reliable. Can you please try again, or try sending a test email to see if it works that way?


      1. Leny

        I guess the issue may be then for using the free plan. Cause I also did a test send, and it did not work


  2. Bechir Arfaoui

    an SSJS into an email is a nice quick solution but at scale it is risky.

    I see it performing better if you make it run in a Journey with a custom activity that stores Chat GPT results into a Data Extension and then uses that Data in the Email send step in the journey.

    The same implementation is possible using Automation Studio with an SSJS step to prepare the user data but keep in mind you risk running into a timeout error if you have a lot of data to be sent to Open AI.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s