You are on page 1of 24

Path to Code

Begin Your Salesforce Coding Adventure


Episode 8
Integrate Salesforce using Rest API
Speaker
Salesforce MVP
Sr Technical Architect
23x Certified
@Jitendra Zaa
Congratulations - MOAR badges - Week 8
Agenda
• Basics of JSON
• What is API
• Introduction to REST API
• Using Postman to consume Rest API
• Using cURL to consume Rest API
• Using Apex to consume Rest API
Some House Rules
• Please mute your mic
• Keep adding questions in Zoom Q&A Window
• No question is too small
• Questions will be answered in last 15 mins
Basics of JSON
● JSON Stands for “JavaScript Object Notation”.
● Language Independent representation of objects
● Parsers widely available in many languages
● Alternative to XML
● JSON can be transmitted over http / https
XML vs JSON
<Person> {
<FirstName>Foo</FirstName> “FirstName” : “Foo”,
<LastName>Bar</LastName> “LastName” : “Bar”
</person> }
Advantage of JSON
● XML is Verbose
● More size than of equivalent JSON representation because of
repeated Tags
● JSON Simple to represent
● Recommended for devices with low processing power or slow
internet
● REST is very common and supports JSON widely.
● REST and JSON are the music of the internet.
● Google , Yahoo, D&B web services exposed as REST with
response type JSON
JSON Guidelines
Key – Value enclosed in double quotes (String).
Value - can again anything of datatype.
{} – represents Object
[] – represents Array
, - Separates data element within Object
Supports basically four data type:
1.Boolean
2.Number
3.String
4.Object
JSON to Apex Class - Deserialization
JSON String :
Person d = (Person )System.JSON.deserialize(jsonString, Person.class);
{
“FirstName” : “Foo”,
“LastName” : “Bar”
}
Equivalent Apex Class:
Class Person
{
public String FirstName;
public String LastName;
}
API
Stands for “Application Programming
Interface”

API
API

API

SOAP API - XML


REST API - JSON
Rest API
Representational State Transfer (REST)

Authenticate

Make Request
Process
Response
Request Type
• Get
• Get resources
• Post
• Create resources
• Put
• Update existing resource
• Delete
• Delete resources
Example 1: Find Stock Rates
• Use Free API From - https://www.alphavantage.co/documentation/
• Authentication done using parameter API Key
• Example -
https://www.alphavantage.co/query?function=GLOBAL_QUOTE&sy
mbol=ibm&apikey= M47PP7VKAUI80TSC
Tools Available
• cURL
• Postman
• and many more
Sample Apex Code
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.alphavantage.co/query?func
tion=SYMBOL_SEARCH&keywords=BA&apikey=demo');
request.setMethod('GET');
HttpResponse response = http.send(request);
String body= response.getBody() ;
System.debug(body);
Trailhead Modules

Apex Integration Services Lightning Platform API Basics


Reference
• JSON vs XML
• Online JSON Editor
• JSON Serialization vs Deserialization
• Free API to check Stock Status
Q&A
Next Episode 9
Integration 101 - Making SOAP Callout
Thanks

You might also like