The Force.com REST API lets you integration with Force.com applications using standard HTTP methods. Using Apex REST you can expose the data that resides into your Salesforce instance and you can also get the data directly from the other system which hits the particular URL.
You can use below HTTP methods to expose the Salesforce data and vice versa
GET, POST, DELETE, PUT and HEAD, PATCH
In this post, I’ll show you how to create an Apex Rest API which will expose Salesforce data which do not require authentication.
We will use Salesforce Site to create the API, using site will let us use APEX REST without authentication from any platform.
1 – Create a new Custom Object for this tutorial. Setup -> Create -> Objects -> New Custom Object -> Create New Object Student
2 – Now, we will create an Apex Class which will expose Student__c Object. Setup -> Develop -> Apex Classes -> New and copy the Apex code from Here and paste into Editor.
3 – Now, time to create a Salesforce Site. Setup -> Develop -> Sites. You will need a domain for the site and I will use studentAPI (You need to enter a valid domain) and Check Availabilty. Check I have read and accepted the Force.com and then Register My Force.com Domain.
4 – Click New to create new Site
5 – After Saving You will be redirected to Site Detail Page which will look like below and Click on “Public Access Settings”
public access setting – Settings which can be access publically without login credentials. So, We will add our Apec Class in public access settings.
6 – Find “Enabled Apex Class Access”
7 – Edit, Find StudentController in Available Apex Classes and then Add into Enabled Apex Classes then Save it.
8 – Time to give permission to the Object (Student__C) that we have created earlier in the tutorial. Edit the Profile and find Student Object under Custom Object Permissions provide Read, Create, Edit and Delete Permission to Student Object. Save the Profile.
9 – Now time is to test the APEX REST API. We will use https://www.hurl.it/ tool to test the same.
10 – Before we go for testing, we will need Endpoint URL to hit the APEX REST. For this, go to Setup -> Develop -> Sites -> Copy Site URL and paste into Notepad or any editor.
Note: – Site URL must be started with https no matter what is shown.
11 – We have copied the Site URL, however, this is not the complete. We will need to add some extra string that we used in our Apex class with services/apexrest/. Open apex class and copy the string after urlMapping.
12 – Now, we will create the complete URL which will look like the combination of Site URL + /services/apexrest/ + Urlmapping String. After you will merge all three string URL will look like
12 – Time for some magic open https://www.hurl.it/. Select Post For Method, Paste the complete URL. Headers – Add content Type as JSON. Click add body and use JSON for the body. Launch Request to create Student into Salesforce
“Name” : “Anonymous Student”
13 – You will get the response like below after successful creation of the record.
14 – Open Salesforce, go to student object and you will get the record there
15 – We have created Student record, now let’s fetch that record using the API. Create request like below and then launch request.
Response will be like below
Hope you enjoyed reading. If you have any question or issue please let me know in the comment section.