Start building your own REST API in Salesforce using Apex

Salesforce platform is amazing and its lets your create your own lightweight  REST API using Apex. You can use this REST API to access f...

Salesforce platform is amazing and its lets your create your own lightweight  REST API using Apex. You can use this REST API to access data, by authenticating the caller/consumer of API using standard oAuth and return data format support JSON/XML, purely depends how you want return back the data

I personally call this concept as custom REST web services to make it sound simpler, but how different is the REST API than a web service ? Well, the edge of using building REST API over web services is that, API enjoys pleasant features of REST architecture which is object mapping which mean that REST reads the data-tables (data model) for you and supports object mapping, that is actually the (process of transforming data in one form and transforming into other), so you once properly authenticated, you can access data through URL.

REST in apex supports basic CRUD (create, read, update and delete) operations, to create rest API of your own you need to define the set of operation that is supported over http. After writing REST API we can enjoy the full support of REST behavior.

To declare the rest API, first step is define the endpoints, you can define a class as endpoint, which in other words mean this class is exposed as a web service and can be accessed through a url for the authenticated call.

Lets create a connected application on the first step to make an authentication for requester. Go to Setup and create a new connected application, as shown in picture, for now lets give full access to requester

Connected App

Apex Endpoints :

Lets define the Apex endpoint, basically the address which we call from the url, for demo the Apex Endpoint here is Oyecode, so lets create a class with the name OyeCodeController and that perform two operations

1. Create a contacts
2. Pull back the contact, if provided with Id

Here is the Apex Endpoint

Lets create special class that handle response you receive from API called ResponseHandler, this allow use to drive the information which we want to return back once API is requested.

I am defining ResponseClass with five attributes

Now clearly our response class will look like this

Once we have structure ready for API response, lets start carving class that use response.

In this example, I am performing the two basic operation with API  '@HttpPost and @HttpGet' so I am creating two separate methods for handling operation here

Below is the @HttpPost Method to create new contact

Here is @HttpGet method to retrieve contact by Id

So clearly, I wrote custom methods on top of this retrieve contact using SOQL and inserting a new records, here is full blown view of our freshly built API

Accessing API

Lets start using the API we just built, there are multiple way you can access API but easier way is using curl (which some folks may not like it) but I have written a template code here that can be reused to access Salesforce API

Testing  : @HttpGet

Curl Command to generate access token through connected app using command line tool, to generate your curl command check this post 

On sending the request using curl or hurl you see this response on matching id on other favorite rest client

This is how simply you can create a custom API in Salesforce and I recommend reading these post as well for more details

1. Apex Rest on by Peter Knolle
2. Building Rest API by Jeff Douglas

What Others Are Reading