Skip to content

How To Test Salesforce Rest Api Using Postman

API Testing Blog

Getting Started with Salesforce REST API Testing using Postman

Postman is a powerful tool for testing APIs, and Salesforce’s REST API offers a versatile way to interact with your Salesforce data. This guide will walk you through the process of testing Salesforce REST API endpoints using Postman, providing practical examples to get you started.

Prerequisites

  • Salesforce Developer Account: You’ll need a Salesforce developer account to access the API.
  • Postman: Download and install the latest version of Postman from https://www.postman.com/.
  • Basic Knowledge of REST API Concepts: Familiarity with HTTP methods (GET, POST, PUT, DELETE), headers, and authentication is helpful.

Setting up Postman for Salesforce API Testing

  1. Create a New Request: Open Postman and create a new request.
  2. Configure the Request Method: Select the appropriate HTTP method for your API call. For example, GET for retrieving data, POST for creating new records, PUT for updating records, and DELETE for deleting records.
  3. Enter the Endpoint URL: The URL for Salesforce REST API endpoints generally follows this pattern:
    https://<instance>.salesforce.com/services/data/v<version>/<object>/
    Replace the placeholders with:
    • : Your Salesforce instance (e.g., ‘na1’, ‘eu1’).
    • : The API version (e.g., ‘54.0’).
    • : The name of the Salesforce object you’re working with (e.g., ‘Account’, ‘Contact’).

      Example:

      https://na1.salesforce.com/services/data/v54.0/Account/
      1. Authentication: To access Salesforce data, you’ll need to authenticate. Postman supports various authentication mechanisms. We’ll use OAuth 2.0, which is the recommended authentication method for Salesforce REST API:

      a. Create a Connected App:

      • Navigate to Setup in your Salesforce org.
      • Search for “Apps” and select “Connected Apps.”
      • Click “New” to create a new Connected App.
      • Configure the app details (App Name, API Name, Contact Email, etc.).
      • Under “API Enabled,” choose “Enable OAuth Settings.”
      • Specify the Callback URL (this can be any valid URL, as we’ll be using Postman’s built-in OAuth functionality).
      • Under “Selected OAuth Scopes,” choose the relevant scopes (e.g., “Full Access (full),” “API Only (api)”).
      • Save the connected app.

      b. Generate an OAuth 2.0 Client Credentials Flow:

      • In Postman, click on the “Authorization” tab.

      • Select “OAuth 2.0” as the type.

      • Fill out the following fields:

        • Grant Type: “client_credentials”
        • Token Name: Choose a name for your token (e.g., “SalesforceToken”)
        • Auth URL: https://login.salesforce.com/services/oauth2/token
        • Client ID: The Consumer Key from your Salesforce connected app.
        • Client Secret: The Consumer Secret from your Salesforce connected app.
        • Scope: Enter the same scopes you selected in your Connected App (e.g., “full” or “api”).
      • Click “Get New Access Token.” Postman will automatically get an access token for you.

      1. Set the Authorization Header:
      • Once you have an access token, Postman will automatically set the Authorization header with “Bearer <access_token>“. This header is crucial for authenticating your API requests.

      Testing Salesforce REST API Endpoints

      Now that you have set up authentication, you can start testing various endpoints:

      1. Retrieving Data (GET)

      Retrieve All Accounts:

      • Method: GET
      • URL: https://na1.salesforce.com/services/data/v54.0/Account/
      • Headers: Authorization with your access token.

      Example Response:

      {
      "totalSize": 2,
      "done": true,
      "records": [
      {
      "attributes": {
      "type": "Account",
      "url": "/services/data/v54.0/sobjects/Account/001D000000XgXbHIAU"
      },
      "Name": "Acme Corporation",
      "Id": "001D000000XgXbHIAU",
      "BillingStreet": "123 Main St"
      // ... more fields
      },
      {
      "attributes": {
      "type": "Account",
      "url": "/services/data/v54.0/sobjects/Account/001D000000XgXbKIAU"
      },
      "Name": "Example Inc.",
      "Id": "001D000000XgXbKIAU",
      "BillingStreet": "456 Elm St"
      // ... more fields
      }
      ]
      }

      Retrieve a Specific Account:

      • Method: GET
      • URL: https://na1.salesforce.com/services/data/v54.0/Account/001D000000XgXbHIAU
      • Headers: Authorization with your access token.

      2. Creating a New Record (POST)

      Create a New Contact:

      • Method: POST
      • URL: https://na1.salesforce.com/services/data/v54.0/sobjects/Contact/
      • Headers: Authorization with your access token, Content-Type: application/json
      • Body:
      {
      "FirstName": "John",
      "LastName": "Doe",
      "Email": "john.doe@example.com",
      "AccountId": "001D000000XgXbHIAU"
      }

      Example Response (showing the newly created record’s ID):

      {
      "id": "003D000001O1x0JIAS",
      "success": true,
      "errors": []
      }

      3. Updating a Record (PUT)

      Update an Existing Contact’s Email:

      • Method: PUT
      • URL: https://na1.salesforce.com/services/data/v54.0/sobjects/Contact/003D000001O1x0JIAS
      • Headers: Authorization with your access token, Content-Type: application/json
      • Body:
      {
      "Email": "john.doe@updated.com"
      }

      4. Deleting a Record (DELETE)

      Delete a Contact:

      • Method: DELETE
      • URL: https://na1.salesforce.com/services/data/v54.0/sobjects/Contact/003D000001O1x0JIAS
      • Headers: Authorization with your access token.

      Testing Queries (SOQL)

      You can also use Postman to test Salesforce Object Query Language (SOQL) queries. The SOQL endpoint is:

      • URL: https://na1.salesforce.com/services/data/v54.0/query/

      Example: Get all Accounts with a specific name:

      • Method: GET
      • URL: https://na1.salesforce.com/services/data/v54.0/query/?q=SELECT+Id,Name+FROM+Account+WHERE+Name+LIKE+%27Acme%25%27
      • Headers: Authorization with your access token.

      Important Considerations

      • API Versioning: Salesforce regularly updates its API. Ensure you’re using a compatible API version for your Salesforce instance.
      • Rate Limits: Salesforce has restrictions on the number of API calls you can make within a given timeframe. Monitor your calls to avoid exceeding the limits.
      • Error Handling: API calls can fail. Pay attention to the HTTP status codes and error messages returned in the responses.
      • Security: Always use authentication to secure your API calls and prevent unauthorized access.

      Conclusion

      Postman provides an intuitive and efficient way to test and interact with Salesforce REST API endpoints. By following these steps, you can successfully test your Salesforce integrations and ensure your code interacts with your Salesforce data as expected.

      API Testing Blog