Try any Salesforce API quickly with Curl command templates

by - 10:27 PM

Working with tons of application here at Salesforce, I got a chance work with wide gamut of internal and externally exposed API's. Over the period of time, I developed a curl template, that I used in command prompt or Mac terminal to consume api in no time. I treat this template as my key resource to connect to Salesforce api's in matter of minutes and then move on writing code in more proper fashion for designing the client.

Create a connected Application in Salesforce 

To understand the templates better, go your salesforce org, login and create a connected application in 
   'Setup -> Apps -> Connected Apps' 
Grab the Consumer Key and Consumer Secret after creating application, save them, before we move on. Now we can use these credentials to authenticate the api-user and get API Token, once we have API token the last step would be to access the API to pull data we want.

Download curl here for (windows and mac)

(1) Curl API Authentication Template - for Salesforce API Authentication 

Copy this code snippet shown below in sublime or your favorite text editor and replace with your login credentials (consumer key, consumer secret, username, password) and then paste the template on command prompt in mac terminal to receive the API token in the response from REST API. I used Iterm mac terminal in my machine. Once your post the request, then see if, the response you get after posting, matches with the one shown in this snippet here.

I'll use this authentication and data-access curl template for two of the popular salesforce application programming interfaces, Salesforce Analytics API and Salesforce Tooling API

How to get started with Salesforce Analytics API ?

Awesome, so we have the access to API now, lets us kickstart with Salesforce Analytics API and pull a report details, salesforce analytics api can be used to bring all analytics of transaction you made on top of salesforce, lets say you want to represent data by pulling from a Salesforce report in form of Chart/Dashboard based on javascript, you can use Analytics API to pull data.

Jump in here on documents for Salesforce Analytics API (using REST) and  have a look how you can pull the report metadata  by providing the report Id. I created a matrix report on Opportunity and I'll use the report ID to pull metadata of this report. Remember the response would be in the JSON format.

Reading docs, it clearly says you can pull data for this matrix report by requesting Salesforce analytics
Api in the url format shown here

This GET request, /services/data/v29.0/analytics/reports/00OD0000001ZbP7MAK/describe, to the Describe resource returns metadata for a matrix report. This includes a bucket field, groupings, summaries, and a custom summary formula.

(2) Curl Data Access Template - for accessing data from Analytics API

Replace API Token with your token and run this command prompt again like I did here in the picture.

Bingo - you get the JSON body response, you are ready to rock now

Copy paste and go to JSON beautifier to beautify the response from this junk this format to beautiful looking JSON response like here

Brilliant, we did it !! Now let us create new connected application to pull data from Salesforce Tooling API. Now, I am going to pull the 'list of all sobjects' but with tooling api.

How to get started with Salesforce Tooling API ?

Tooling API can be used to retrieve meta data information about salesforce elements, like finding the code coverage of the class, list of objects and all meta-data of salesforce structures. You can use this api to modify existing salesforce tools : Read more about Tooling API here 

Lest get the list of SObjects in my current Salesforce org. Now, to get the list of sobjects in your org, you need to pass this params in the url
Lists the available Tooling API objects and their metadata.

Brilliant, using the 'Template (1)' as shown above, I replaced my credentials again I replaced both consumer key and client secret' by creating a brand new connected application from scratch

Lets Authenticate using Login Template

Now once you get API token, lets query Tooling API by through this url
Result is exciting

Sooner, I'll share the sample application I built on top of Salesforce that uses salesforce and remote objects to pull data plugging into API you needed. I hope this template will helpful for you in playing and spreading Salesforce API love all over world and among the community.

Sharing is Caring 

You May Also Like