Access Models API with REST

The Models REST API connects your application to large language models (LLMs) for chat, embeddings, text generation, and feedback.

Before you make a request, complete the shared authentication flow to create a Salesforce app and generate a JSON Web Token (JWT). See Authenticate to the Models API. Then use your token to call the endpoints described here.

note: All Models API requests are subject to Salesforce’s usage and billing rates for Einstein Requests. See Agentforce and Generative AI Usage and Billing and the Rate Card for Einstein Requests.

Postman Collection

The quickest way to get started with the Models REST API is with our Postman collection.

Call the Models REST API

When you have a valid token, you can call any of the Models REST API endpoints.

Pick a Capability

The Models REST API provides four different endpoints for four different capabilities.

Capability Endpoint Path Description
Generate Chat /models/{modelName}/chat-generations Generate a response based on a list of messages representing a chat conversation.
Generate Embeddings /models/{modelName}/embeddings Create an embedding vector representing the input text.
Generate Text /models/{modelName}/generations Generate a response based on the prompt provided.
Submit Feedback /feedback Submit feedback for generated text.

Choose a Model

Choose a model to use when you call the generations or embeddings endpoints. Each endpoint, other than the feedback endpoint, requires the API name of the model as part of the path. For instance, if you want to use the OpenAI GPT-4o mini model for a generation request, specify the API name sfdc_ai__DefaultOpenAIGPT4OmniMini by making a request to this endpoint.

https://api.salesforce.com/einstein/platform/v1/models/sfdc_ai__DefaultOpenAIGPT4OmniMini/generations

The Models API supports models from various Salesforce-enabled providers. See Supported Models.

Provide Headers

These headers are relevant for API requests.

Header Description Example
Authorization Required. Authorization method for this request. This header must contain your JWT. Bearer {token}
Content-Type Required. Specify the application/json content type. application/json
x-sfdc-app-context Required. This value is reserved for future use. For now, specify EinsteinGPT. EinsteinGPT
x-client-feature-id Required. This value is reserved for future use. For now, specify ai-platform-models-connected-app. ai-platform-models-connected-app

Make a Request

Make a POST request to one of the Models REST API endpoints. This example demonstrates a call to the generations endpoint.

curl --location 'https://api.salesforce.com/einstein/platform/v1/models/sfdc_ai__DefaultOpenAIGPT4OmniMini/generations' \
--header 'Authorization: Bearer ••••••' \
--header 'Content-Type: application/json' \
--header 'x-sfdc-app-context: EinsteinGPT' \
--header 'x-client-feature-id: ai-platform-models-connected-app' \
--data '{
    "prompt": "James Lee is a financial advisor, living in San Diego. His phone number is 867-5309. Generate a story about his work and life."
}'

tip: If you receive a 401 or 404 response code, verify that your Salesforce app includes all the required scopes and settings, and that you're using the complete access_token value that you generated. See Authenticate to the Models API.

And here’s a sample response.

{
  "id": "chatcmpl-9AMuFltdq7M5ntZVvQcAkgyWhfoas",
  "generation": {
    "id": "333127bd-2d5d-41e8-9781-59a1a18ed69f",
    "generatedText": "Once upon a time in sunny San Diego, there lived a diligent and experienced financial advisor named James Lee. With a passion for numbers and a knack for helping others achieve their financial goals, James had built a reputation as a trusted advisor in the community.\n\nJames had always been fascinated by the world of finance. From a young age, he would spend hours poring over financial newspapers and studying the stock market. As he grew older, his passion only intensified, leading him to pursue a degree in finance from a prestigious university...",
    "contentQuality": {
      "scanToxicity": {
        "isDetected": false,
        "categories": [
          {
            "categoryName": "identity",
            "score": 2.0e-5,
          },
          {
            "categoryName": "hate",
            "score": 0.0,
          },
          {
            "categoryName": "profanity",
            "score": 3.0e-5,
          },
          {
            "categoryName": "violence",
            "score": 2.0e-5,
          },
          {
            "categoryName": "sexual",
            "score": 1.5e-4,
          },
          {
            "categoryName": "physical",
            "score": 0.0,
          }
        ]
      }
    },
    "parameters": {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null
    }
  },
  "moreGenerations": null,
  "prompt": null,
  "parameters": {
    "created": 1712258359,
    "usage": {
      "completion_tokens": 607,
      "prompt_tokens": 39,
      "total_tokens": 646
    },
    "model": "gpt-35-turbo",
    "system_fingerprint": null,
    "object": "chat.completion"
  }
}

Next Steps

After you successfully call the API, refer to these topics to optimize your implementation.

See Also