An OpenAI API key does not go into a custom setting, a field or a line of Apex. Salesforce has a place made for it: an External Credential, used through a Named Credential. Set up this way, the key is stored encrypted by Salesforce, nobody can read it back from the screen, and Salesforce adds it to each request on its own.
This guide shows every click. It takes about ten minutes and needs no code. The same steps work for Anthropic and Azure OpenAI; only two values change, and they are in a table further down.
What You Need
- A Salesforce login that can open Setup (the gear icon, top right).
- An OpenAI API key. Go to platform.openai.com/api-keys, click Create new secret key and copy it. The account needs a payment method or credit.
One point that saves confusion: the API is separate from the ChatGPT app. A ChatGPT Plus or Team subscription does not include API usage. The API is billed per token on your OpenAI API account.
Keep the key private. You will paste it into Salesforce once. Do not send it by email or chat.
Step 1: Create the External Credential
The External Credential is where the key lives.
- In Setup, type
Named Credentialsin Quick Find and click Named Credentials. - Click the External Credentials tab, then New.
- Enter Label
OpenAI, NameOpenAI, and choose Authentication Protocol: Custom. - Click Save.

You are now on the page of the new credential. Add two things:
- Under Principals, click New. Type
Defaultin Parameter Name and save. - Under Custom Headers, click New. Enter Name
Authorizationand ValueBearer, one space, then your key, for exampleBearer sk-abc123.... Save.
After saving, Salesforce hides the header value. That is expected.
Step 2: Create the Named Credential
The Named Credential holds the address and points at the External Credential.
- Click the Named Credentials tab, then New.
- Enter Label
OpenAI, NameOpenAIand URLhttps://api.openai.com. - Keep Enabled for Callouts on.
- In External Credential, choose
OpenAI. - If a managed package will make the calls, scroll down to Allowed Namespaces for Callouts and type the package's namespace. For AI Engine that is
CloudConnected. - Click Save.

Use only the base URL. The code that makes the call adds the path.
The namespace line is the one people miss. Salesforce does not let a managed package use a credential you created unless its namespace is listed there.
Step 3: Give People Access
A key in an External Credential cannot be used by anyone until a permission set says so.
- In Setup, open Permission Sets and click New.
- Enter a label such as
AI Provider Access, leave License on --None-- and save. - Click External Credential Principal Access, then Edit.
- Move
OpenAI - Defaultto the right and save. - Click Manage Assignments, then Add Assignment, and assign it to everyone who will use the AI features, yourself included.
Step 4: Use the Credential
What you do next depends on what calls OpenAI.
Your own Apex. Call the endpoint as callout:OpenAI/v1/.... Salesforce resolves the address and adds the Authorization header. The key never appears in your code.
AI Engine. Open the Ai Agent Admin Setup tab, go to Org Profile, click + New Profile, choose OpenAI (Responses API) as the provider and type OpenAI in Named Credential. The name must match exactly, capital letters included. Then choose which objects and fields the AI may read. The full sequence, with the page setup, is in the Quick start.
To see what the result looks like before you set anything up, try the interactive demo. It runs the AI Engine component on sample data.
Anthropic and Azure OpenAI
The steps are the same. Two values change.
| Provider | Header name | Header value | URL |
|---|---|---|---|
| OpenAI | Authorization |
Bearer YOUR_KEY |
https://api.openai.com |
| Anthropic | x-api-key |
YOUR_KEY |
https://api.anthropic.com |
| Azure OpenAI | api-key |
YOUR_KEY |
https://YOUR_RESOURCE.openai.azure.com |
Azure OpenAI can also sign in without a key, with a Microsoft Entra ID service principal. See Azure OpenAI in Salesforce.
If It Does Not Work
These are the messages AI Engine shows, and what each one means.
| Message | Cause |
|---|---|
| "AI provider is not configured" | The name typed in the Org Profile is not the Named Credential's Name, or the namespace is missing from Allowed Namespaces for Callouts |
| "AI provider access is not configured for your user" | The user is not in the permission set from step 3 |
| "AI provider returned an error" | The key is wrong or expired, the word Bearer or the space is missing, or the OpenAI account has no credit |
| "AI provider timeout" | The provider did not answer in time. If it happens every time, check the URL |
Keeping the Key Safe
- Rotate it. Create a new key in OpenAI, open the External Credential, edit the custom header and paste the new value. Nothing else changes.
- One key per org. Use a different key for a sandbox and for production, so you can revoke one without touching the other.
- Set a spending limit in your OpenAI account.
- Check retention before real data. By default OpenAI can keep API requests for up to 30 days for abuse monitoring. For customer data, ask OpenAI for zero data retention, or use Azure OpenAI in your own tenant. See Zero data retention with your AI provider.
Key takeaway: an OpenAI key belongs in an External Credential, reached through a Named Credential and opened to users by a permission set. Three objects, about ten minutes, and the key never appears in code or on a screen again.
Frequently Asked Questions
Where do I put an OpenAI API key in Salesforce?
In an External Credential, as a custom header named Authorization with the value Bearer followed by the key. A Named Credential with the URL https://api.openai.com points at it, and a permission set gives users access to its principal.
Do I need a Remote Site Setting for api.openai.com?
No. A Named Credential allows the callout by itself. Remote Site Settings are only needed for callouts that do not go through a Named Credential.
Can users see the API key?
No. After you save the custom header, Salesforce hides its value. Users with access to the principal can make calls through the credential, but cannot read the key.
Does a ChatGPT subscription cover API usage from Salesforce?
No. ChatGPT subscriptions cover the ChatGPT app. Calls from Salesforce use OpenAI's API, which is billed per token on your API account.
What is Allowed Namespaces for Callouts?
A field on the Named Credential that lists which managed packages may use it. Without the package's namespace there, Salesforce blocks the package from a credential you created. For AI Engine the namespace is CloudConnected.
How do I replace the key later?
Create a new key in your OpenAI account, edit the custom header on the External Credential and paste the new value. The Named Credential, the permission set and the application settings stay as they are.
See AI Engine on your own data
Native Salesforce package. Your own OpenAI, Claude or Azure key. 5-day free trial, no credit card.
Book a 30-min demo