How to Call Salesforce.com Rest APIs
Rest APIs are quite easy to use and my preferred way to get things done. This post outlines how you can enable your SFDC instance to accept REST calls, and demonstrates fetching the data using Postman .
Create a Connected App #
Create a connected app that tells SFDC that a valid client will try to access the REST APIs.
Go to Setup | App Manager. Click on ‘New Connected App’.
Enter the following parameters:
- Connected App Name and API Name - whatever makes sense
- Contact Email - ditto
- Check OAuth Settings - we will use OAuth for authentication
- Update Callback URL to “https://login.salesforce.com”, or anything that catches your fancy
- In “Selected OAuth Scopes”: select and add “Full Access”. Remember that this is for testing alone
Save, and wait for 2-10 min as instructed. Make a note of the ‘Consumer Key’ (client_id) and ‘Consumer Secret’ (client_secret) when the process completes. (i.e., you no more see the instructional text on the page, and both the above fields are populated).
Make REST API calls using Postman #
Open Postman. Click ‘New Request’ in the opening screen, or click New | Request.
We authenticate before doing anything else.
In ‘Authorization’ tab: select ‘Type’ as ‘OAuth2’. Click ‘Get New Access Token’. Enter following details:
- Grant Type: Password Credentials
- Access Token URL: https://login.salesforce.com/services/oauth2/token
- Username
- Password (password + Salesforce security token)
- Client Id: client_id noted earlier
- Client Secret: client_secret noted earlier
- Client Authentication: Send client credentials in body
Click Request Token to generate a new token. Postman shows a popup on successful token generation. Note the ‘Instance URL’ - this is your SFDC base URL e.g. https://crmcog-dev-ed.my.salesforce.com. Click on Use Token once the token is generated to copy over the token to the main screen.
In the main screen, enter “instance_url” + parameters to do a ‘get’ operation. I just used a simple query - ‘SELECT ID, NAME FROM ACCOUNT LIMIT 10’.
Click ‘Send’ button to see the output.
Explore https://workbench.developerforce.com/restExplorer.php to know more about what you can do with REST services.