Skip to content

How To Use Postman With Salesforce

API Testing Blog

Harnessing Postman for Salesforce API Testing: A Comprehensive Guide

Postman, a powerful tool for API testing and development, can significantly streamline your Salesforce API testing process. This guide will walk you through the essential steps, equipping you with the knowledge to effectively test Salesforce APIs using Postman.

1. Setting Up Postman for Salesforce API Testing

Before diving into the practical examples, let’s set up our Postman environment for Salesforce API interactions.

Step 1: Install Postman

Download Postman from https://www.postman.com/ and install it on your system. Postman offers both a desktop application and a web-based version.

Step 2: Authorize Your Salesforce Instance

To interact with your Salesforce instance, you’ll need to set up OAuth 2.0 authorization in Postman. Here’s how:

  1. Create a Connected App: Navigate to Setup > App Manager > New Connected App in your Salesforce instance.
  2. Define App Details: Provide a name, API name, and a contact email.
  3. Enable OAuth Settings: Set the following options:
    • Callback URL: https://www.getpostman.com/oauth2/callback
    • OAuth Scopes: Select the required scopes for your API calls (e.g., api, web).
  4. Generate Consumer Key and Secret: Generate a consumer key and secret using the “Generate Consumer Key and Secret” button.
  5. Save the Connected App: Save the Connected App settings.

Step 3: Configure Postman for OAuth 2.0 Authentication

  1. Open Postman: Launch the Postman application.
  2. Create a New Request: Click the “New” button and select “Request.”
  3. Add an Environment Variable: Click the “Environment Variables” button (eye icon) and create a new environment. Add the following variables:
    • client_id: The consumer key generated in Step 2.
    • client_secret: The consumer secret generated in Step 2.
    • redirect_uri: The callback URL (from Step 2).
    • auth_url: https://login.salesforce.com/services/oauth2/authorize
    • token_url: https://login.salesforce.com/services/oauth2/token

2. Making Simple Salesforce API Calls with Postman

Let’s begin with a simple example: retrieving a list of contacts.

Step 1: Create the Request

  1. Request Method: Choose “GET” for retrieving data.
  2. Request URL: Enter the Salesforce API endpoint: https://[yourInstance].salesforce.com/services/data/v[version]/sobjects/Contact. Replace [yourInstance] with your Salesforce instance’s domain and [version] with the desired API version (e.g., 49.0).
  3. Authorization: Select “OAuth 2.0” from the “Authorization” tab.
  4. Configure OAuth 2.0: In the “Configure” dialog, choose “Get New Access Token.”
  5. Grant Access: A pop-up window will appear asking for permission to access your Salesforce data. Grant the required permissions.

Step 2: Send the Request

Click the “Send” button to execute the request.

Step 3: Analyze the Response

Postman will display the API response in the “Body” tab. The response will typically be in JSON format, providing information about the retrieved contacts.

{
"totalSize": 2,
"done": true,
"records": [
{
"attributes": {
"type": "Contact",
"url": "/services/data/v49.0/sobjects/Contact/0032800001Q0RWyAAO/"
},
"Id": "0032800001Q0RWyAAO",
"FirstName": "John",
"LastName": "Doe",
// ... other contact fields
},
{
// ... another contact
}
]
}

3. Testing Salesforce API Operations with Postman

Postman supports various Salesforce API operations, enabling comprehensive testing. Let’s explore some examples.

3.1 Creating a New Contact

Step 1: Create the Request

  1. Request Method: Choose “POST” for creating a new contact.
  2. Request URL: Use the same URL as before: https://[yourInstance].salesforce.com/services/data/v[version]/sobjects/Contact.
  3. Authorization: Use OAuth 2.0 authorization as explained earlier.
  4. Request Body: Add the contact information in the request body as JSON:
{
"FirstName": "Alice",
"LastName": "Smith",
"Email": "alice.smith@example.com"
}

Step 2: Send the Request

Click “Send” to submit the request and create the contact.

Step 3: Analyze the Response

The response will contain information about the newly created contact, including its ID:

{
"attributes": {
"type": "Contact",
"url": "/services/data/v49.0/sobjects/Contact/0032800001Q0RWzAAO/"
},
"Id": "0032800001Q0RWzAAO",
"FirstName": "Alice",
"LastName": "Smith",
"Email": "alice.smith@example.com"
}

3.2 Updating an Existing Contact

Step 1: Create the Request

  1. Request Method: Use “PATCH” for updating a contact.
  2. Request URL: Enter the contact’s URL: https://[yourInstance].salesforce.com/services/data/v[version]/sobjects/Contact/[contactId]. Replace [contactId] with the ID of the contact you want to update.
  3. Authorization: Use OAuth 2.0 authorization.
  4. Request Body: Provide the updated contact details in JSON format:
{
"FirstName": "Alice",
"LastName": "Johnson"
}

Step 2: Send the Request

Execute the request to update the contact.

Step 3: Analyze the Response

The response will confirm the successful update and may include the updated contact details.

4. Leveraging Postman Collections for Salesforce API Testing

Postman Collections allow you to organize and structure your API tests. They are particularly valuable for complex scenarios involving multiple API calls.

Step 1: Create a New Collection

In Postman, click “New” and select “Collection.” Give your collection a name (e.g., “Salesforce API Tests”).

Step 2: Add Requests to the Collection

Drag and drop the requests you created earlier into the collection. You can create new requests specifically for your collection as well.

Step 3: Define Variables and Environments

Collections allow you to manage environment variables and define shared variables for your requests. Use variables to store sensitive information like your Salesforce instance, usernames, or API versions.

Step 4: Utilize Collection Runners

Postman’s Collection Runner enables you to execute and test your collection’s requests sequentially or in a specific order. You can set up tests and assertions within the collection runner to verify the responses.

5. Advanced API Testing Techniques with Postman

Postman offers advanced features that enhance your Salesforce API testing capabilities.

5.1 Assertions: Assertions allow you to verify specific conditions in your API responses. For instance, you can assert that the status code is 200 (success) or that the response body contains a specific value.

5.2 Pre-Request Scripts: Pre-request scripts execute before sending each request within a collection. They are useful for manipulating data, setting up headers, or dynamically generating request parameters.

5.3 Test Scripts: Test scripts run after each request and allow you to perform more complex validation, such as comparing timestamps or verifying data consistency.

5.4 Mocks: Mocks enable you to simulate API responses, enabling offline testing or testing in environments where the actual Salesforce API is unavailable.

Conclusion

Postman is a powerful tool that can elevate your Salesforce API testing process. By mastering the techniques outlined in this guide, you can ensure the quality and reliability of your Salesforce integrations while significantly streamlining your testing workflow. Remember to leverage Postman’s advanced features, such as assertions, pre-request scripts, and mocks, to maximize your API testing efficiency.

API Testing Blog