๐ŸŒ Using Salesforce REST API: A Beginnerโ€™s Guide - Solution for Guru

Table of Contents
< All Topics
Print

๐ŸŒ Using Salesforce REST API: A Beginnerโ€™s Guide

The Salesforce REST API allows you to access and manipulate Salesforce data programmatically using standard HTTP methods. Itโ€™s lightweight, easy to use, and ideal for integrating Salesforce with external applications, web apps, or custom tools.


๐Ÿ“Œ What is the Salesforce REST API?

The REST API (Representational State Transfer) provides a simple, JSON-based interface to interact with Salesforce objects and data.

You can:

  • Retrieve data from Salesforce
  • Create, update, and delete records
  • Run SOQL queries
  • Perform searches and analytics

๐Ÿ”‘ Prerequisites

Before using the REST API, you need:

  • Salesforce Edition with API access (Enterprise, Developer, Performance, Unlimited)
  • A Connected App set up in Salesforce for authentication
  • API-enabled user profile
  • A REST API tool like Postman or cURL

๐Ÿงฐ Step 1: Create a Connected App

  1. Go to Setup โ†’ App Manager
  2. Click New Connected App
  3. Fill in:
    • Name
    • Contact Email
  4. Under API (Enable OAuth Settings):
    • Check Enable OAuth Settings
    • Set a Callback URL (e.g., https://login.salesforce.com/services/oauth2/success)
    • Select scopes like:
      • Access and manage your data (api)
      • Perform requests on your behalf at any time (refresh_token)
  5. Save and note your Consumer Key and Consumer Secret

๐Ÿ” Step 2: Authenticate (OAuth 2.0 Flow)

Send a POST request to:

https://login.salesforce.com/services/oauth2/token

With the following parameters:

grant_type = password
client_id = YOUR_CONSUMER_KEY
client_secret = YOUR_CONSUMER_SECRET
username = YOUR_USERNAME
password = YOUR_PASSWORD+SECURITY_TOKEN

๐Ÿ“Œ Note: Never share your credentials or tokens in public.

A successful response returns an access token and instance URL.

๐ŸŒŸ Step 3: Make API Calls
๐Ÿ” Example 1: Retrieve a Record (GET)

GET /services/data/v59.0/sobjects/Account/001XXXXXXXXXXXX
Host: yourInstance.salesforce.com
Authorization: Bearer YOUR_ACCESS_TOKEN

๐Ÿ“ Example 2: Create a Record (POST)
POST /services/data/v59.0/sobjects/Contact/
Host: yourInstance.salesforce.com
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json

{
  "FirstName": "John",
  "LastName": "Doe",
  "Email": "john.doe@example.com"
}

๐Ÿ”„ Example 3: Run SOQL Query

GET /services/data/v59.0/query/?q=SELECT+Name,+Industry+FROM+Account
Host: yourInstance.salesforce.com
Authorization: Bearer YOUR_ACCESS_TOKEN

๐Ÿ’ก Tips for Beginners

Use Postman to test endpoints and explore request/response formats.

Refer to the Salesforce Object Reference for field names and API names.

Always check API version compatibility (e.g., /v59.0/).

Handle rate limits and token expiration gracefully.

Use refresh tokens for long-term access without logging in each time.

๐Ÿ™‹ FAQs

Q: Can I use the REST API in Salesforce Essentials?
A: No, API access is not available in Salesforce Essentials.

Q: Do I need coding experience to use the REST API?
A: Basic familiarity with HTTP and JSON is helpful. Tools like Postman reduce the need for coding.

Q: Whatโ€™s the difference between REST and SOAP in Salesforce?
A: REST is lighter, uses JSON, and is easier for web-based apps. SOAP is XML-based and suited for enterprise systems.