This document outlines how to connect to the Salesforce API in order to retrieve Salesforce data based on the Salesforce query API. You will find a list of cURL examples that can be reused with minimal changes required.
Authorization
The Salesforce API requires the use of the OATH Authentication workflow. This process includes multiple authentication API calls in order to initialize the API. The following commands outline these authorization calls in order.
Auth Step 1 - OATH Authorize
The OATH Authorization step requires users to authenticate using their personal credentials in order to retrieve an OATH Client ID and Client Secret.
curl -G -X GET "https://[SALES_FORCE_ENV_ID].my.salesforce.com/services/oauth2/authorize" \
--data-urlencode "client_id=[YOUR_CLIENT_ID]" \
--data-urlencode "scope=api refresh_token" \
--data-urlencode "redirect_uri=https://[YOUR_K3_BASE_URL]/oauth2callback" \
--data-urlencode "response_type=code" \
--data-urlencode "state=851cf15b-6de6-4848-af1d-c406e65b5d7c"
Auth Step 2 - OATH Token
The OATH Token step takes the Client API credentials (Client ID & Client Secret) in order to retrieve and API Token and Refresh Token.
curl -X POST "https://[SALES_FORCE_ENV_ID].my.salesforce.com/services/oauth2/token" \
--data-urlencode "client_id=[YOUR_CLIENT_ID]" \
--data-urlencode "client_secret=[YOUR_CLIENT_SECRET]" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "code=aPrx9YW5jCZKCNwLtenbZk.NZCV03iraH9SskucJTP1lwRX.wsa4ueUMDLeD.K_X1ubNjOwfSg==" \
--data-urlencode "redirect_uri=https://[YOUR_K3_BASE_URL]/oauth2callback"
Auth Step 3 - OATH Request
The OATH Request step takes the refresh Token in order to retrieve a new Token.
curl -X POST "https://[SALES_FORCE_ENV_ID].my.salesforce.com/services/oauth2/token" \
--data-urlencode "client_id=[YOUR_CLIENT_ID]" \
--data-urlencode "client_secret=[YOUR_CLIENT_SECRET]" \
--data-urlencode "grant_type=refresh_token" \
--data-urlencode "refresh_token=[REFRESH_TOKEN]"
Data Calls
Getting Leads
This Salesforce API call retrieves new Leads added to Salesforce. The query string (q
) includes a WHERE
clause that limits the query results to leads created after April 1, 2023 at 12:00:00 AM UTC. You can modify the date and time in this clause to retrieve leads created after a different date and time.
Note: In this command, you will need to replace <yourinstance>
with the instance name of your Salesforce organization and <your_access_token>
with an actual access token that you have generated for your Salesforce account. You will also need to replace XX.X
with the version of the Salesforce API you are using (e.g., 52.0).
curl https://yourinstance.salesforce.com/services/data/vXX.X/query/?q=SELECT+Id,+FirstName,+LastName,+Email,+Phone,+Company+FROM+Lead+WHERE+CreatedDate+>+2023-04-01T00:00:00Z \
-H "Authorization: Bearer <your_access_token>" \
-H "Content-Type: application/json" \
-X GET
Sample Results
{
"totalSize": 2,
"done": true,
"records": [
{
"attributes": {
"type": "Lead",
"url": "/services/data/v52.0/sobjects/Lead/00Q1r00001AbCdEFgh"
},
"Id": "00Q1r00001AbCdEFgh",
"FirstName": "John",
"LastName": "Doe",
"Email": "[email protected]",
"Phone": "555-555-5555",
"Company": "Acme Corp"
},
{
"attributes": {
"type": "Lead",
"url": "/services/data/v52.0/sobjects/Lead/00Q1r00001AbCdEFij"
},
"Id": "00Q1r00001AbCdEFij",
"FirstName": "Jane",
"LastName": "Smith",
"Email": "[email protected]",
"Phone": "555-555-5555",
"Company": "Widgets Inc"
}
]
}