---
schema_version: "1"
id: fd0100c8c5b75b02a441dfc01262bc644b52727a032616c968cec5854aa33e1b
title: Apex REST Basic Code Sample
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_rest_code_sample_basic.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_basic.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: e2edd87e8036ae1a44436ea7de93b42894c4e021f83a5aec543968f0b854d410
description: This sample shows how to implement a simple REST API in Apex with three HTTP request methods to delete, retrieve, and update a record.
---
# Apex REST Basic Code Sample

This sample shows how to implement a simple REST API in Apex with three HTTP request methods to delete, retrieve, and update a record.

For more information about authenticating with cURL, see the [Quick Start](https://developer.salesforce.com/docs/atlas.en-us.264.0.api_rest.meta/api_rest/quickstart.htm "HTML (New Window)") section of the REST API Developer Guide.

1. Create an Apex class in your instance from Setup. Enter Apex Classes in the Quick Find box, select **Apex Classes**, and then click **New**. Add this code to the new Apex class:
    
    @RestResource(urlMapping='/Account/\*')
    global with sharing class MyRestResource {
    
        @HttpDelete
        global static void doDelete() {
            RestRequest req = RestContext.request;
            RestResponse res = RestContext.response;
            String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            Account account = \[SELECT Id FROM Account WHERE Id = :accountId WITH USER\_MODE\];
            delete as user account;
        }
      
        @HttpGet
        global static Account doGet() {
            RestRequest req = RestContext.request;
            RestResponse res = RestContext.response;
            String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            Account result = \[SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId WITH USER\_MODE\];
            return result;
        }
      
      @HttpPost
        global static String doPost(String name,
            String phone, String website) {
            Account account = new Account();
            account.Name = name;
            account.phone = phone;
            account.website = website;
            insert as user account;
            return account.Id;
        }
    }
2. To call the doGet method from a client, open a command-line window and execute the following cURL command to retrieve an account by ID:
    
    curl -H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"
    
    - Replace sessionId with the <sessionId\> element that you noted in the login response.
    - Replace instance with your <serverUrl\> element.
    - Replace accountId with the ID of an account which exists in your organization.
    
    After calling the doGet method, Salesforce returns a JSON response with data such as the following, where accountId is a placeholder value:
    
    {
      "attributes" : 
        {
          "type" : "Account",
          "url" : "/services/data/v22.0/sobjects/Account/accountId"
        },
      "Id" : "accountId",
      "Name" : "Acme"
    
    }
    
    ![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)
    
    #### Note
    
    The cURL examples in this section don't use a namespaced Apex class so you don’t see the namespace in the URL.
3. Create a file called account.txt to contain the data for the account you will create in the next step.
    
    {
      "name" : "Wingo Ducks",
      "phone" : "707-555-1234",
      "website" : "www.wingo.ca.us"
    }
4. Using a command-line window, execute the following cURL command to create a new account, where sessionId and instance are placeholder values:
    
    curl -H "Authorization: Bearer sessionId" -H "Content-Type: application/json" -d @account.txt "https://instance.salesforce.com/services/apexrest/Account/"
    
    After calling the doPost method, Salesforce returns a response with data such as the following, where accountId is the ID of the account you just created with the POST request:
    
    "accountId"
5. Using a command-line window, execute the following cURL command to delete an account by specifying the ID, where sessionId, instance, and accountId are placeholder values:
    
    curl —X DELETE —H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"