
In this blog post, we will be discussing the development of a Lightning Web Component (LWC) that utilizes the OpenAI image generation API to generate images based on a user’s input. We will also discuss how this LWC can be integrated into Salesforce CRM to enhance the user experience.
First, let’s take a look at the OpenAI image generation API. The API allows developers to generate images based on a user’s input, known as a prompt. The prompt can be any text that describes the desired image. For example, a prompt of “A dog playing fetch” would generate an image of a dog playing fetch. The API returns the generated image in the form of a URL, which can then be displayed in the LWC.

To develop the LWC, we utilized the Salesforce Lightning Web Components framework. The LWC consists of an HTML file, a JavaScript file, and a CSS file. The HTML file includes a form that allows users to input their prompt, as well as a button to submit the prompt to the API. The JavaScript file handles the logic for sending the prompt to the API, receiving the generated image URL, and displaying the image in the LWC.
The OpenAI image generation API requires an API key to access the service. You can get the API key from the OpenAI site. To keep the API key secure, we can store it in a custom configuration file that is not committed to version control OR Salesforce custom setting or custom metadata type.
Since Salesforce does not permit making API calls from JavaScript code, we need to create CSP-trusted sites within the Salesforce platform in order to allow such functionality.
To create CSP Trusted Sites in Salesforce, you will need to follow these steps:
- Log in to your Salesforce org as an administrator.
- Go to Setup > Security > CSP Trusted Sites.
- Click on the “New Trusted Site” button.
- In the “URL” field, enter the URL (https://api.openai.com) of the API endpoint that you want to make calls to.
- Select the appropriate options for “Allow in” (e.g. “Inline Scripts”, “Scripts from URLs”, etc.) based on your use case.
- Click “Save” to add the URL to your CSP Trusted Sites.
Once the LWC is developed, we can integrate it into Salesforce. The LWC can be added to any Salesforce page, such as a Home page, an account, or a contact page, by using the Salesforce Lightning App Builder. This allows users to easily access the LWC and generate images based on their inputs.

This LWC allows users to make API calls to OpenAI’s image generation service. This means that users can input a text prompt and the component will generate an image based on that prompt. This opens up a wide range of possibilities for how the component can be used in Salesforce CRM. Some of the key use cases include:
- Generating custom images for marketing materials: The LWC can be used to generate images based on prompts or text inputs provided by the user. These images can be used for marketing materials such as social media posts, email campaigns, and website banners.
- Enhancing user interface: The LWC can be used to add dynamic images to Salesforce records, making the user interface more visually appealing and engaging. This can be particularly useful for records such as accounts, contacts, and leads.
- Customizing product images: The LWC can be used to generate custom images of products based on user inputs. This can be useful for businesses that sell customizable products and want to show customers how the finished product will look.
- Creating unique images for blog post: The LWC can be used to generate unique images for blog post, this can be useful for businesses that want to create original and engaging images to use in their blog content.
- Creating images for internal communications: The LWC can be used to create images for internal communications such as company newsletters, announcements, and training materials.
- Creating images for chatbot: The LWC can be used to create images for chatbot, this can be useful for businesses that want to create engaging images to use in their chatbot conversation.
- Creating images for training materials: The LWC can be used to create images for training materials, this can be useful for businesses that want to create engaging images to use in their training materials.
- Creating images for customer service: The LWC can be used to create images for customer service, this can be useful for businesses that want to create engaging images to use in their customer service interactions.
In addition to these use cases, the component can also be customized and integrated into other Salesforce functionality, such as creating images for custom objects, or being used as a standalone component in a community or portal. The possibilities are endless, and the component is a valuable tool for businesses that want to add a creative and dynamic touch to their Salesforce CRM.
Check out the code in this GitHub repository.

In conclusion, the LWC that we developed utilizing the OpenAI image generation API can be a valuable addition to Salesforce CRM. It allows users to easily generate images based on their inputs, which can be used for various purposes such as creating marketing materials or adding visual elements to records. By utilizing Salesforce’s Lightning Web Components framework and properly securing the API key, we were able to create a secure and user-friendly solution.
About the blog
SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:
Newsletter
Subscribe to our email newsletter to be notified when a new post is published.