In this blog we are going to learn about how to expose an Apex method  as a REST resource using @HttpGet annotation.


–> REST annotation enables you to expose an Apex class or an Apex method as a REST resource. In salesforce, there are six REST annotations, that is :

1). @RestResource(urlMapping=’/your url’)

2). @HttpGet

3). @HttpPost

4). @HttpPut

5). @HttpPatch

6). @HttpDelete

–> Classes with REST annotation should be defined as global.

–> Methods with REST annotation should be defined as global static.


–> @HttpGet is used at method level. This method is called when a HTTP GET  request is sent. To expose your apex class as REST resource you should use @RestResource(urlMapping=’/your url’) annotation at class level as shown below.

global class restExample {
    global static void exampleMethod() {
        // Write your code here 

–> Here url mapping is relative to

–> If your class is in namespace say ‘abc’, then API  url must be as follows:


–> URL are case-sensitive.


Let’s create an account by sending a parameter through API url.

Step 1: Create an apex class  say ‘restExample’.

global class restExample {
    * Webkul Software.
    * @category  Webkul
    * @author    Webkul
    * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (
    * @license
    // Method to get the Account name from parameter
    global static void getAccountName() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        String accountName = req.params.get('account_name');
        // Call the method to create Account
        if(accountName != null){
        } else {
	// Create Account when param is set 
    public static void createAcc(String accName) {
            Account acc = new Account(Name = accName, Description = 'Account is created by REST API');
            insert acc;
        } catch (Exception ex) {
    // Create Account when param is not set 
    public static void createAcc() {
            Account acc = new Account(Name = 'Name is not set', Description = 'Account is created by REST API');
            insert acc;
        } catch (Exception ex) {

Step:2: Now, to expose above rest class create a site as shown below. Goto>> SetUp>> Develop>> Sites

# Register your domain:

# Create New Site 

# Copy the Default Web Address

# Now goto Public Access Setting :

# Provide Access to object which you want to expose in class.

# Expose Apex class :

That’s all for setting up of site.

Step 3:  Now paste  Default Web Address(which you have copied from site setting) by appending:

1). Without sending any parameter : ‘/services/apexrest/example’ . 

2).  With parameter : ‘/services/apexrest/example?account_name=Webkul’

Now, goto account page and verify your results.




That’s all for @HttpGet Annotation In Salesforce, still if you have any further query feel free to contact us, we will be happy to help you

Leave A Reply

Please verify that you are not a robot.

Tell us about Your Company

How can we help you with your business?

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home