Saturday, September 24, 2022

How to connect to your salesforce org with postman?

 What is Postman?

Postman is an API platform that is used for building and testing API's. This is very special tool for salesforce developer to test the api request and response.

Now, let us setup salesforce first.

Firstly we need to create a connected app in salesforce.

What is "Connected App" in salesforce?

For an external application that needs to authenticate with Salesforce we need to create a connected app so as to inform Salesforce about the new authentication entry point. Connected app uses standard OAuth 2.0 protocol to authenticate.

Go to setup > click app manager > "New Connected App" button.

Enter "Connected App Name", "API Name" and "Contact Email".

Click "Enable OAuth Settings".

Enter "Callback URL".

Select "Selected OAuth Scopes" as Manage user data via APIs (api)

Callback URL:

A callback URL is the URL that is invoked after OAuth authorization for the consumer (connected app). In some contexts, the URL must be a real URL that the client’s web browser is redirected to. In others, the URL isn’t actually used, but the value between your client app and the server (the connected app definition) must be the same. 

In our case as we are using postman the callback URL will not impact us and we can write anything here, in our case we will use https://www.salesforce.com.

How to connect to your salesforce org with postman?


Now, click on the "Consumer Key and Secret" --> "Manage Consumer Details" button.

Note down the "Consumer Key" and "Consumer Secret".

How to connect to your salesforce org with postman?


Now, let us setup postman.

Once you download the postman app, install it.

We are using "username password flow" to connect to salesforce org.

First step is to get access token. 

If we are using production org we need to call the below URL to get access token with POST method.

https://login.salesforce.com/services/oauth2/token

If we are using sandbox org we need to call the below URL to get access token with POST method.

https://test.salesforce.com/services/oauth2/token

How to connect to your salesforce org with postman?

grant_type       --> Must be the keyword "password" for this authentication flow.

client_id        --> The Consumer Key from the connected app definition.

client_secret    --> The Consumer Secret from the connected app definition.

username         --> End-user’s username.

password         --> End-user password +  Security token

As an example, if password is YYYYY and security token is ZZZZZ then the value that will be

passed under password is YYYYYZZZZZ .

Once you click the "Send" button you will receive the response as shown below.

{
    "access_token""",
    "instance_url""https://myknowndomain-dev-ed.my.salesforce.com",
    "id""",
    "token_type""Bearer",
    "issued_at""",
    "signature"""
}

Now, copy the "access_token" and "instance_url" as this will be required to make further api calls.

Now, let us try to do a GET call to get the account information.

In "Headers" block specify the Authorization as "Bearer accesstokenValue".

Ensure to provide space between Bearer and access token value.

The part of the URL "https://myknowndomain-dev-ed.lightning.force.com" shown in below image on which we are doing GET call is nothing but "instance_url" obtained during POST call.

The complete URL looks like below,

https://myknowndomain-dev-ed.lightning.force.com/services/data/v50.0/query/?q=SELECT+Name,Description+From+Account

How to connect to your salesforce org with postman?


In this way we have successfully integrated postman and salesforce.

No comments:

Post a Comment