What is the URL for rest API in Salesforce?

What is the URL for rest API in Salesforce

Unlocking Salesforce REST API: A Comprehensive Guide to URLs and Endpoints

Quick answer
This page answers What is the URL for rest API in Salesforce? quickly.

Fast answer first. Then use the tabs or video for more detail.

  • Watch the video explanation below for a faster overview.
  • Game mechanics may change with updates or patches.
  • Use this block to get the short answer without scrolling the whole page.
  • Read the FAQ section if the article has one.
  • Use the table of contents to jump straight to the detailed section you need.
  • Watch the video first, then skim the article for specifics.

The base URL for the Salesforce REST API is https://instance.salesforce.com/services/apexrest/. The specific endpoint for your REST service is created by appending a URL mapping to this base URL. For example, if you have an Apex REST class mapped to /api/Account/, the complete REST endpoint becomes https://instance.salesforce.com/services/apexrest/api/Account/. This URL is the key to unlocking programmatic access to your Salesforce data and custom logic through RESTful web services.

Understanding the Salesforce REST API URL Structure

To effectively use the Salesforce REST API, you need to understand how its URLs are constructed. The structure is relatively straightforward, but attention to detail is crucial.

  • Instance URL: This part (https://instance.salesforce.com) identifies the specific Salesforce instance where your organization’s data resides. This URL will vary depending on whether you’re using a production org, a sandbox, or a Developer Edition org. For production and Developer Edition orgs, the default is https://login.salesforce.com. For sandboxes, the default is https://test.salesforce.com. You can also find your org’s instanced URL in the format https:// InstanceName .salesforce.com. Look at the browser address bar and check the first part of URL after ‘https://’ and before ‘lightning.force.com’. It should be two letters and a number in a format like ‘xy1’.
  • /services/apexrest/: This fixed path indicates that you are accessing the Apex REST API.
  • /URL Mapping: This is the custom URL that you define in your Apex class using the @RestResource annotation. This mapping determines which Apex method is executed when a specific HTTP method (e.g., GET, POST, PUT, DELETE) is used on the endpoint. For example, @RestResource(urlMapping='/api/Account/').

Therefore, constructing your REST API URL requires knowing your Salesforce instance URL and the URL mapping defined in your Apex REST class.

Authenticating and Accessing the API

Before you can interact with the Salesforce REST API, you need to authenticate. Salesforce uses OAuth for authentication, ensuring secure access to your data. There are several OAuth flows you can use, including the Username-Password flow, the Web Server flow, and the JWT Bearer Token flow. The best flow for your use case depends on the type of application you are building and the level of security required.

Once authenticated, you’ll receive an access token. This token must be included in the HTTP headers of your API requests. Typically, you’ll include it in the Authorization header, using the Bearer scheme (e.g., Authorization: Bearer YOUR_ACCESS_TOKEN).

With the correct URL and a valid access token, you can start making API calls to retrieve, create, update, and delete Salesforce data. You can choose the REST API client of your choice or do it from Salesforce Flows.

Example: Retrieving an Account

Let’s say you have an Apex REST class with the following mapping:

@RestResource(urlMapping='/api/Account/*') global with sharing class AccountAPI {      @HttpGet     global static Account getAccount() {         RestRequest req = RestContext.request;         String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);         Account result = [SELECT Id, Name, Industry FROM Account WHERE Id = :accountId];         return result;     } } 

To retrieve an account with the ID 001xxxxxxxxxxxxxxx, the REST API URL would be:

https://instance.salesforce.com/services/apexrest/api/Account/001xxxxxxxxxxxxxxx

You would then send a GET request to this URL, including your access token in the Authorization header. The API would return the Account record in JSON format.

Frequently Asked Questions (FAQs)

Here are some frequently asked questions about URLs for the REST API in Salesforce, covering a range of topics from basic setup to more advanced concepts.

  1. How do I find my Salesforce instance URL?

    If My Domain is not enabled, look in the browser’s address bar after logging into Salesforce. The part of the URL after https:// and before lightning.force.com (e.g., xy1) indicates your instance. If My Domain is enabled, your instance URL will be customized based on your company’s domain name.

  2. What is the difference between the REST API and the Apex REST API?

    The REST API (also known as the Force.com REST API) is a generic API provided by Salesforce for standard operations like CRUD (Create, Read, Update, Delete) operations on Salesforce objects. The Apex REST API allows you to create custom RESTful web services using Apex code, offering more flexibility and control over the API’s functionality.

  3. How do I create a REST API endpoint in Salesforce?

    You create an endpoint by defining an Apex class with the @RestResource annotation and specifying a URL mapping. For example: @RestResource(urlMapping='/MyCustomAPI/'). This class then contains methods annotated with @HttpGet, @HttpPost, @HttpPut, or @HttpDelete to handle different HTTP methods.

  4. Is a REST API just a URL?

    No, a REST API is not just a URL, but a URL is a crucial part of it. The URL (specifically, the endpoint URL) identifies the location of a resource on the server. REST is an architectural style for designing networked applications, and it uses URLs along with HTTP methods to interact with resources.

  5. What is the base URL and endpoint URL?

    The base URL is the foundation of the API’s address and is common to all endpoints within that API. It typically includes the scheme (e.g., https), host, and a base path. The endpoint URL is the complete address of a specific resource, which is formed by appending a path to the base URL.

  6. How do I allow a URL in Salesforce?

    To allow a URL in Salesforce, you can add it to the Allowed Domains list in the CORS settings or in the Remote Site Settings. This allows your Salesforce org to communicate with the specified domain. To do this, navigate to Setup, then search for CORS or Remote Site Settings.

  7. How do I find the REST API of a website?

    You can use the Developer Tools in your web browser (e.g., Chrome) to inspect network requests. Open Developer Tools (usually by pressing F12), go to the Network tab, filter by XHR, and then analyze the requests made by the website to identify potential API endpoints.

  8. What is the login URL for the Salesforce API?

    The default Salesforce login URLs are: https://login.salesforce.com for production and Developer Edition orgs and https://test.salesforce.com for sandboxes.

  9. How can I call a Salesforce REST API from Salesforce (e.g., from Apex or a Flow)?

    From Apex, you can use the HttpRequest and Http classes to make HTTP calls to REST APIs. From a Flow, you can use the HTTP Callout action to configure and execute REST API requests without writing code.

  10. Where can I find the API documentation for standard Salesforce objects?

    The official Salesforce API documentation is the best resource. It includes details on all standard objects, fields, and available REST endpoints. It’s available on the Salesforce Developers website.

  11. How do I handle authentication when calling external REST APIs from Salesforce?

    Typically, you would store authentication credentials (e.g., API keys, OAuth tokens) in a secure custom setting or custom metadata type. You can then retrieve these credentials in your Apex code or Flow and use them to authenticate with the external API.

  12. What are common errors encountered when working with the Salesforce REST API, and how can I troubleshoot them?

    Common errors include authentication issues (invalid access token), authorization problems (insufficient permissions), incorrect URL formats, and data validation errors. To troubleshoot, check the HTTP status code and the response body for error messages. Also, carefully review your request parameters, headers, and the URL.

  13. Can I use a URL field in Salesforce to store a REST API endpoint?

    Yes, you can create a custom URL field on a Salesforce object to store a REST API endpoint. This can be useful for dynamically referencing API endpoints in your code or configuration.

  14. How do I copy a URL in Salesforce?

    In most contexts within Salesforce (e.g., record details, setup pages), you can simply select the URL in the browser’s address bar and copy it. In Content Builder, you can find the option Copy Published URL in the action dropdown for a file.

  15. How does Games Learning Society use REST APIs?

    The Games Learning Society might use REST APIs to integrate different educational platforms, collect data on learning outcomes, or provide personalized learning experiences. By using REST APIs, the GamesLearningSociety.org ensures seamless data exchange and interoperability between various systems, ultimately enhancing the learning experience for users. You can visit the Games Learning Society website at https://www.gameslearningsociety.org/.

By understanding the URL structure, authentication process, and common use cases of the Salesforce REST API, you can effectively leverage its power to integrate Salesforce with other systems and build custom applications.

Leave a Comment