What is REST API?
REST API is an architectural style that we use to make sure 2 way data transfer happen between platforms which are built on different platforms.
REST API is one of the famous methodologies that we use and the other famous variant being SOAP API. Both REST API and SOAP API are completely different when it comes it how they are programmed, how they behave and how they are consumed.
In case you need some audio material related to REST API, to which you can listen to, then, I did a podcast on REST API, it’s Architecture and some other details related to it. Please check it out.
Why do we need REST API?
In this modern connected world, we need our web accounts associated with different vendors to talk to each other, so that we can automate stuff or save time for ourselves or be more productive.
Let’s consider an example which sums it up. I use Instagram a lot and I don’t want to upload a picture once in Instagram and then re-upload the same picture in Twitter and then repeat the same process once more with Facebook. Rather, I want to automate in such a way, if I upload it in Instagram it has to be posted in Twitter and Facebook simultaneously.
For this to happen, I need to make sure my Instagram account talks to Twitter and Facebook account and automates the stuff. There is no way to get this done if Twitter and Facebook platforms doesn’t open up their data using API’s. So every platforms creates REST API and we need to consume them to make sure we get the data from the respective platform or to push new data into them.
As far as Salesforce Platform is concerned, we can create REST API using Apex Programming. We will be able to handover few URL’s to other platform developers and when they consume these URL’s, data rolls out of Salesforce Platform and this data will be utilised in other platforms.
REST API when compared to SOAP API has many advantages and its super duper light weight. That’s the reason it’s preferred a lot, over SOAP API.
How do we create REST API with Apex Programming?
We need to make use of an Annotation called
@RestResource and along with this annotation we need to make use of an attribute called
urlMapping. This attribute accepts the URL pattern so that when a developers makes a call to an API and if it matches the URL pattern in
urlMapping, based on the HTTP method an apex method gets invoked.
This method will have the some logic related to fetching data and other such things.
We will also have an option to response either in XML format or JSON format. As I have mentioned earlier JSON is preferred over JSON for various reasons.
In case things are confusing, I recorded an episode on this topic and you can check it out.