Have Fun with APIs | Learn Salesforce like never Before

Hello, my Name is Smriti Sharan. I am avid blogger and youtuber. I am to aim to make learning Fun!!

Cloudy: I am unable to understand what is an API?

Simmy: Well, not to worry when I am here!

We will walk through

  • What is API
  • Technical Jargon around API – client, server, request, response, http methods, status code
  • Testing of API using Postman
  • Types of API
  • Benefits of API
  • Inbound vs Outbound Integration in Salesforce

Bonus Topics

  • Demo, Demo………Lots of Demo!!
  • Fun Fact
  • Quiz Time
  • Lots and Lots of Fun Filled Images

All in a fun way, like I am 10. So, let’s get started!

Simmy: Let’s look at how MakeMyTrip works.

When you search for flights, have you ever thought about how it shows you flights from different airlines and which seats are available?

 

That’s because MakeMyTrip uses APIs provided by the airlines.

These APIs help MakeMyTrip fetch information about flights and seat availability directly from the airlines’ systems. This way, you can see and book available seats right away.

Essentially, an API, stands for Application Programming Interface, acts as a middleman that transfers data from one place to another. It helps two applications to communicate to each other.

Cloudy: But there is lot of tech jargon around API which I am unable to understand?

Simmy: In the example of Zomato and Google Maps.

Zomato is Client, Google Maps is server. Now Zomato cannot directly interact with Google Maps server so there will a middleman called Google Maps API.

Zomato will send Request to Google Maps API, once Google Maps API has the data then it will send Response to Zomato.

Cloudy: Now that seems clear!

Simmy: Let’s dive a bit deeper. When Zomato needs information, it sends a request using HTTP to what we call an endpoint. Think of an endpoint as a specific address or route on the internet where Zomato can ask for the data it needs.

Essentially, these are endpoints are URLs

Now when Google Maps API get the request, it wants to send the response. That response is not in terms of plain English like ‘Driver is 10 min away’. It sends response in JSON/ XML format.

Cloudy: What will happen if Zomato sends wrong request?

Simmy: Well for that Status Codes come into picture.

A number code that appears in the body of an API response that tells you if your request was successful or not.

The first number in the code represents the category of the status.

If the code starts with a 2, your request was successfully processed. If the code starts with a 4, something went wrong.

Cloudy: Can you show me a demo so I am more clear?

Simmy: Well Demo Time

Install Postman: Postman is a popular API development and testing tool that provides a user-friendly interface for working with APIs.

To get started with Postman, you first need to install it on your machine.

How to make your first API Request

Follow these steps to make a simple GET request:

Click the “+” button to create a new request tab

Screenshot--45-

Enter the API Endpoint: In the URL bar, enter the endpoint of the API you want to interact with. Let’s take fun example of API which displays cat images.

https://http.cat/

Send the Request: Click on the “Send” button to execute the request. Postman will display the response from the API.

Cloudy: Well, that was fun example!

Simmy: Client makes request to APIs using several methods.

Let’s understand with help of Skippy Popsicles

  1. GET: Viewing the current inventory of Skippy Popsicles, including different flavors and quantities.

India's Skippi revamps popsicle ...

  1. POST: Adding a new flavor of popsicle to the inventory system.

Add product - Free commerce and ...

  1. PUT: Replacing an existing popsicle flavor with a completely new one in the inventory.

Product Replacement icon PNG and SVG ...

  1. PATCH: Updating the quantity of an existing popsicle flavor, such as adding 100 more strawberry popsicles to the stock.

OpenCart - Admin Product Quantity Update

  1. DELETE: Removing a discontinued flavor from the inventory system.

Delete Cart Item icon PNG and SVG ...

Cloudy: Can you show this it in action?

Simmy: Demo Time

Let’s use JSONplaceholder for understanding httpMethods. JSONplaceholder is a free online API that developers use for testing and prototyping.

If you need a fake API to play around with, you should check it out.

Endpoints in JSONplaceholder’s API : The URL we are going to be using is https://jsonplaceholder.typicode.com. You will get details of which endpoint to hit in the API documentation.

GET Request : In the input, you would type https://jsonplaceholder.typicode.com/posts. Once you do that and click on ‘Send’ button, you will receive a response in JSON format, with 201 status code.

2. POST /posts. Change the method type to “POST”. Then in the input type https://jsonplaceholder.typicode.com/posts. Post is used to create new data to endpoint.

3.PUT /posts. Change the method type to “PUT”. Then in the input type https://jsonplaceholder.typicode.com/posts. Put is used to update existing data at endpoint.

4. PATCH /posts/{post-id}. Change the method type to “PATCH”. Then in the input type https://jsonplaceholder.typicode.com/posts/111. This will help in creating partial updates to existing records.

8. DELETE /posts/{post-id}

Change the method type to “DELETE”. Then in the input type https://jsonplaceholder.typicode.com/posts/4. Click send. Delete is used to Delete data at the endpoint.

Cloudy: Now that I understand HTTP methods, does this mean I can access any API and retrieve any data?

Simmy: Well, it’s not quite that simple. There are different types of APIs, each with its own access rules:

1. Open APIs – Also known as public APIs, these are available for anyone to use. For example, the cat API, I showed you earlier.

2. Internal APIs – Sometimes called private APIs, these are used within an organization to improve services and data sharing between different internal systems.

secure hash algorithm vector design ...

3. Partner APIs – These are externally available but require specific rights or licenses. For example, Uber and Lyft provide APIs that allow third-party developers to integrate functionalities like ride ordering directly into their apps.

11 Ridesharing Apps to Consider While ...

4. Composite APIs – They streamline processes by making a single API call to retrieve or update data from multiple services at once. For instance, if you’re developing an app for Skippy Popsicles, you might use a Composite API to display a user’s profile, their recent purchases, and loyalty points in one interface by pulling data from different systems.

Web Service APIs (or Web APIs) – These are specifically designed to enable communication between two applications over the web using network protocols like HTTP.

Time for a Fun Fact

APIs were created before the world wide web, hence the special terms used to define APIs that work with the internet i.e. Web Service.

Cloudy: Well, that’s interesting! What is the benefit of using API?

Simmy:

  1. Efficiency: APIs provide ready-to-use functions, so developers can focus more on creating unique features for their applications, enhancing productivity and innovation.

Efficiency Icon Vector Isolated on ...

  1. Security: APIs act as gatekeepers by allowing only certain data to be exchanged between systems. For example, when apps like Uber or Swiggy use Google Maps, they only get access to location data necessary for their services, without direct access to Google’s servers.

Blog | What is the API Economy - Toro Cloud

This setup helps to protect sensitive information and ensures that each system reveals only what’s needed.

  1. User Experience: By integrating various services, APIs provide a smoother, more cohesive user experience. For example, users can book flights, hotels, and rental cars all from a single ‘Book My Trip’ app, which simplifies planning and booking trips.

  1. Cost-Effective: Using APIs can reduce development costs by utilizing existing frameworks.

Saving Time – API Monetisation ...

Cloudy: That’s really fascinating! But how does all this tie into Salesforce?

Salesforce - Wikipedia

Simmy: Great question, Cloudy! In Salesforce, integrating with external systems can be categorized into two types: callin and callout integrations.

Callout: It involves bringing data from external systems into Salesforce by making HTTP request.

For example, imagine Skippy Popsicles receives orders through Amazon. You can set up Salesforce to automatically create new opportunities whenever a new order is placed.

Callin: It involves sending /exposing data from Salesforce to other systems.

Let’s say Skippy Popsicles wants whenever a new lead is generated in Salesforce, lead information is automatically sent to Mailchimp to send marketing emails.

Cloudy: Well, can we see a real time example of it?

Simmy: Let’s understand callout Integration with the help of Weather API.

For this purpose, we will take example of Weather API.

Review the API documentation to understand which endpoint you need to access the needed resource.

If you go to API Explorer, you will see once you put API key, paramters

API key for Authentication

WeatherAPI gives an API Key to be able to access its resources.

What Is an API Key? Benefits, Best ...

An API key is a unique identifier that you include in API requests to verify you are who you say you are and gain access to the data provided by the API.

You see a url you need to hit to get the response body

Now that you know the end point url,api key, parameters, Let’s execute it in Salesforce.

For this demo Purpose let’s use Anonymous window

Add the code

Code Explaination

HttpRequest req = new HttpRequest(): This line creates a new HTTP request object named req. This object is used to specify the details of the HTTP request to be made, such as the endpoint URL, HTTP method, headers, and body.

req.setEndpoint: This line sets the endpoint URL for the HTTP request. It specifies the URL of the WeatherAPI endpoint along with the API key (key) and the city name (q) for which weather data is requested.

req.setEndpoint(‘http://api.weatherapi.com/v1/current.json?key=4fb4d331ba9742e4b4c180809240105&q=Delhi’);

req.setMethod(‘GET’): This line sets the HTTP method for the request. In this case, it sets the method to GET, indicating that the request is retrieving weather data from the server.

Http http = new Http(): This line creates a new HTTP object named http, which is used to send the HTTP request.

HTTPResponse res = http.send(req): This line sends the HTTP request specified by the req object using the send() method of the http object. It returns an HTTPResponse object res, which contains the response from the server.

Result

Look at Response Body

To have more fun with different APIs, you can check this link

Cloudy: Well, now I am so clear on the concept of API. Can learning be more fun?

Simmy: I hope you enjoyed the learning. Ready for a quick quiz to test what you’ve learned?

Kya_Banegare_Crorepati___kbc funny ...

Question of the Day: Which HTTP method is used to retrieve data?

    • A) POST
    • B) GET
    • C) DELETE
    • D) PATCH

Drop your answer in the comments below and let’s see how many of you can get it right! Lets learn together.

Cloudy – If you enjoyed this video then like the article and Subscribe to the channel to see more fun videos like this.

Feel free to connect with me on:

References

https://jsonplaceholder.typicode.com/

https://www.semrush.com/blog/http-status-codes/

Canva diagram

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes

Leave a Reply

Your email address will not be published. Required fields are marked *