Sub topic 1
REST basics and when to write an Apex REST service
Prerequisites
This chapter assumes you already know the following:
- Basic Apex syntax, classes and static methods
- SOQL and DML fundamentals
- HTTP basics: methods, headers, status codes, request and response bodies
- JSON structure
- Salesforce profiles and permission sets
You don't need to have written a REST service before. This chapter explains the model and helps you decide whether to build one.
What REST means in practice
REST is a style for web APIs built around resources. A resource is a thing a client can name, such as an account. Each resource has a URL. The client acts on it with a standard HTTP method.
| HTTP method | Typical meaning | Example |
|---|---|---|
| GET | Read a resource | Fetch one account |
| POST | Create a resource | Add a new account |
| PUT | Replace a resource | Overwrite an account |
| PATCH | Update part of a resource | Change an account's industry |
| DELETE | Remove a resource | Delete an account |
The server returns a status code that says what happened, such as 200 for success, 201 for created, 404 for not found, and 400 for a bad request. The body usually carries JSON. Each request stands alone, so the server keeps no session state between calls.
Where Apex REST fits
Salesforce already exposes data through the standard REST API. It lets a client query records, create them and update them. Those endpoints live under /services/data/. Apex REST lets you add your own endpoints. They live under /services/apexrest/.
An Apex REST service is an Apex class. Annotations on the class and its methods tell the platform which URL and HTTP method each method handles. Salesforce handles routing, authentication and JSON conversion. You write the logic.
The URL of a deployed service has this shape:
https://MyDomainName.my.salesforce.com/services/apexrest/v1/Accounts/001XXXXXXXXXXXXXXXThe part after /services/apexrest/ comes from the URL mapping you define on the class. You will define it in the next chapter.
When to write a custom service
Start with the standard REST API. Write an Apex REST service only when it clearly falls short. These cases usually justify custom code:
- One call must do several things. A client would otherwise make a chain of standard API calls. Examples are creating an account, its contacts and a task together. A custom endpoint can do all of it in one request and one transaction. If a step fails, the whole transaction rolls back.
- Business logic belongs on the server. Validation, calculations or routing rules should run in one place, whichever client calls.
- You want a simpler contract. Callers get a small, stable JSON shape. They don't need to know your object and field structure.
- The standard API doesn't support the operation. For example, you may need a custom action that is not a plain create, update or delete.
Prefer the standard REST API when it already does the job. Standard endpoints are documented, versioned by Salesforce and maintained by Salesforce. A custom service is code you must test, secure and maintain yourself.
Common mistakes at this stage
- Reaching for Apex too early. Many integrations work fine with standard endpoints. Check them first.
- Treating the service as open. Callers must authenticate. Your code must also respect sharing and permissions. A later chapter covers both.
- Designing around Apex instead of resources. Name URLs after things, such as
/v1/Accounts/, not actions, such as/v1/doStuff.
What comes next
The next chapter shows how to turn a class into a resource with @RestResource. Later chapters cover HTTP method annotations, request and response objects, JSON, status codes, security, tests and versioning.
Exercise
A mobile app needs to create an Account, three Contacts and an Opportunity when a user completes a form. It must either save all of them or none. Would you use the standard REST API or a custom Apex REST service, and why?
Show answer
A custom Apex REST service. One request can run all the inserts in a single transaction, so a failure rolls back every record. With the standard API, the app would make several calls and would have to handle partial failures itself.
Exercise
A client only needs to read one Account by Id and update its Phone field. Should you write an Apex REST service?
Show answer
No. The standard REST API already supports reading and updating a record by Id. A custom service would add code to test and maintain without adding value.