Salesforce REST API Tutorial
Salesforce provides a native REST Api that enables external apps to read / manipulate the data located in Salesforce.
In this tutorial, we will see how to connect to REST API and send a request to create an account.
We will use Postman for testing. you can however choose the REST API client of your choice.
Step 1 : Create a connected app in Salesforce
- Open Setup home -> Apps -> Manage apps -> new connected app
- Set the Name, API Name and contact Email
- Enable OAuth Settings
- Set the callback URL to sfdc://oauth/restapi/success
- Select the scope : “Access and manage your data (api)”
- Save the connected app and wait few minutes for it to be activated.
- Note the consumer key and consumer secret to use them in the next step
Step 2 : Connect to the REST API
To authenticate to the REST API, we use the OAuth Username-Password flow.
- Create a new Postman collection
- Create a new Request, name it “Authentication request”and add it to the collection created previously.
Configure the request as follows :
- Method : POST
- Endpoint : https://login.salesforce.com/services/oauth2/token (replace login by test if connecting to the sandbox)
- Headers
- Content-Type : application/x-www-form-urlencoded
4. Body
- grant_type : password
- client_id : paste the consumer key from the connected app
- client_secret : paste the consumer secret from the connected app
- username : Salesforce username
- password : Salesforce password
5. Hit send
Salesforce returns the response that contains the access token and instance_url note these 2 values to use in the next step.
Step 3 : Manipulate data with REST API
In this step we create an account in Salesforce with the previously obtained access_token and instance_url,
Create an account
- Method : POST
- Endpoint : <instance_url>/services/data/v20.0/sobjects/Account
Headers :
- Authorization : Bearer <access_token>
- Content-Type : application/json
Body :
- The body must be in JSON format
- To create a new account with the Name “Test REST API Account” paste the following JSON in the body
{
"Name" : "Test REST API Account"
}
Body Part :
- Send the request.
After the request is sent, a response is returned by Salesforce with the ID of the new account created.
Let’s see the new account created directly in Salesforce :
Conclusion
The REST API enables you to access (read, update, delete, export…) all data present in Salesforce from an external app, to dig deeper, read the REST API developer guide :