Create public API in Salesforce using apex web services.

Posted on December 9, 2020 Post Thumbnail

Hi Trailblazers, In this blog post I'm going to create a Public API in Salesforce using Apex Web Services.

Before Start, Let's have a look at Demo,  Click on the following link.

Click to Make a Get Request

In the above demo, We are making a Get request from our browser.

What is Public Api?

  An open API (often referred to as a public API) is a publicly available application programming interface that provides developers with programmatic access to a proprietary software application or web service. APIs are sets of requirements that govern how one application can communicate and interact with another.  

In simple words, We don't require any authentication to make a callout.

What is Apex Web Service?

Apex Web Service provides us functionality to expose our Apex class methods as a REST or SOAP web service.

By making our methods callable through the web, our external applications can integrate with Salesforce to perform operations.

In this blog post, We are going to expose our class as a REST web service.

Let's Start

It's super easy to Expose a Class as a REST Service, All you need to do.

1. Define your class as global.

2. Define methods as global static.

3. And add annotations to the class and methods.

Here's a sample code.

@RestResource(urlMapping='/Account/*')
global class MyRestResource {
    @HttpGet
    global static Account getRecord() {
        // Add your code
    }
}

For class : @RestResource(urlMapping='/Account/*')

For methods :

Annotation Action Details

@HttpGet ===>>> Reads or retrieves records.

@HttpPost ===>>> Creates records.

@HttpDelete ===>>> Deletes records.

@HttpPut ===>>> Typically used to update existing records or create records.

@HttpPatch ===>>> Typically used to update fields in existing records.

REST Endpoint for Above class

https://yourInstance.salesforce.com/services/apexrest/Account/

Now, Let's create a class.

@RestResource(urlMapping='/Account/*')
global class heySfOpenApiClass {

}

Create a method to get accounts from Salesforce.

@HttpGet
    global static String getAccounts() {
        RestRequest request = RestContext.request;
        list<Account> result = new list<Account>();
        result =  [SELECT Name,Website FROM Account];
        return 'Get Request Working';
       // return JSON.serialize(result);
    }

Create a method to create an account in Salesforce.

@HttpPost
    global static Id getAccounts(String AccountName) {
        Account acc = new account();
        acc.Name = AccountName ;
        acc.Website = 'www.'+ AccountName +'.com' ;
        Insert acc ;
        return acc.Id;
    }

Final Class with both methods.

@RestResource(urlMapping='/Account/*')
global class heySfOpenApiClass {
    @HttpGet
    global static String getAccounts() {
        RestRequest request = RestContext.request;
        list<Account> result = new list<Account>();
        result =  [SELECT Name,Website FROM Account];
        return 'Get Request Working';
       // return JSON.serialize(result);
    }
    @HttpPost
    global static Id getAccounts(String AccountName) {
        Account acc = new account();
        acc.Name = AccountName ;
        acc.Website = 'www.'+ AccountName +'.com' ;
        Insert acc ;
        return acc.Id;
    }
}

We are done with the Apex code.

Now we'll make them accessible with authentication using the Salesforce Site.

What is Salesforce Sites?

Salesforce Sites enables you to create public websites and applications that are directly integrated with your Salesforce organization—without requiring users to log in with a username and password. ... You can publicly expose any information stored in your organization through a branded URL of your choice.

Let's Register Site in Salesforce Org.

Setup > Site > Register

First, check availability and then register for the same.

First, check availability and then register for the same.

  

Now, Create a new site.

Create a Site

Edit Public Access Site.

Edit Public Access Site

Enabled Apex Class Access

And Give access to newly created Classes.

Enabled Apex Class Access

Everything is set up now.

Final Endpoint Url will be.

heysite-developer-edition.ap16.force.com/heySalesforce/services/apexrest/Account/

Site Domain URL: http://heysite-developer-edition.ap16.force.com/

Site Name: heySalesforce

Apex Service: services/apexrest/Account/

Now, Let's Test it.

I'll use Postman for testing.

Endpoint: https://asifaly-developer-edition.ap15.force.com/BookObject/services/apexrest/Account/

For GET Request

GET

For POST Request

POST

As you can see, It's working now.

This is how we can create a Public API in Salesforce using Apex Web Services.

I hope It helped you somehow.

If you have any question Ask Me

Thanks for Reading:

Write a comment for suggestions and hit the heart icon.


10273
3

Tags: #salesforce

Comments

  • Orlando Avatar
    Orlando - 9 months ago
    Hi, very cool article! I have applied all steps still I get a FORBIDDEN error <Errors> <Error> <errorCode>FORBIDDEN</errorCode> <message>You do not have access to the Apex class named: MyService</message> </Error> </Errors> I gave permissions over the APEX class on Public Access Settings, not only to the site profile but to other to try to troubleshoot the error but still getting the same, what would you advise? Thanks!

    Asif Ali Avatar
    Asif Ali - 7 months ago
    I think integration user doesnt have access to class

  • suraj Avatar
    suraj - 1 month ago
    did not getting list of data through API

Your message is required.

Get notified of new posts