Create Anonymous Salesforce Apex REST API with Force.com Platform

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

1

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.

2

4 – Click New to create new Site

3

5 – After Saving You will be redirected to Site Detail Page which will look like below and Click on “Public Access Settings”

4

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”

5

7 – Edit, Find StudentController in Available Apex Classes and then Add into Enabled Apex Classes then Save it.

6

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.

7

8

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.

9

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. 

10

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

https://studentapi-developer-edition.ap4.force.com/services/apexrest/v1/Students/

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”
}

11.png

13 – You will get the response like below after successful creation of the record.

12.png

14 – Open Salesforce, go to student object and you will get the record there

13

15 – We have created Student record, now let’s fetch that record using the API. Create request like below and then launch request.

14.png

Response will be like below

15

 

Hope you enjoyed reading. If you have any question or issue please let me know in the comment section.

Resources: –

An_Introduction_to_Force.com_Sites

 

Advertisements

8 thoughts on “Create Anonymous Salesforce Apex REST API with Force.com Platform

  1. Santiago G April 15, 2019 / 4:21 pm

    Thanks, it’s a very interesting post, do you know if any further Salesforce limits apply to this case? I mean, does any additional limits could apply using a Site instead of normal Rest Api connection? (such as max callouts per day, etc)

    Like

    • Amit Singh April 17, 2019 / 11:15 am

      Hi Santiago, Yes the limits will be same as for internal salesforce because we are making the api callins from other system to salesforce

      Like

      • Mallik December 28, 2018 / 7:09 am

        Thank you for the content. I have followed your step and I want to test my code now but I am not able to access the website https://www.hurl.it/. Could you help me on that.

        and I request you to post many more posts as you explain them really well and it helps may more beginners like me.

        Thanks a ton.

        Like

      • Amit Singh December 30, 2018 / 4:31 am

        You can install DXC OR RestLest Chrome Extensions for the same

        Like

  2. Sunil June 18, 2018 / 3:29 am

    Thanks for the great Article 🙂

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s