Learn how the @RestResource annotation exposes an Apex class as a REST web service in Salesforce. It maps HTTP methods to class methods and returns JSON or XML, enabling seamless integrations with external systems. A concise look at REST endpoints in Salesforce.

Multiple Choice

Which annotation is used to expose an Apex class as a REST web service?

The annotation that is used to expose an Apex class as a REST web service is @RestResource. This annotation allows developers to define a class as a RESTful web service, enabling the class to handle HTTP requests and deliver responses in various formats such as JSON or XML. By utilizing @RestResource, the methods within the class can be mapped to specific HTTP methods such as GET, POST, PATCH, or DELETE, and can be accessed via a designated URL. The choice to use @RestResource is essential for creating REST APIs in Salesforce, as it acts as the entry point for RESTful interactions with the Apex class. This facilitates the integration of Salesforce with other systems, allowing for seamless data access and manipulation. The other options, while related to handling HTTP requests, do not serve the specific purpose of exposing an Apex class as a RESTful service. For instance, @HttpGet and @HttpPost are annotations that indicate methods that respond to GET and POST requests, respectively, but they need to be used within a class that is annotated with @RestResource to function correctly in a REST API context. The @ServiceEndpoint annotation is more relevant to SOAP web services rather than RESTful services.

Salesforce sits at the crossroads of customer data and custom business logic, a place where you can shape how systems talk to each other without writing a mountain of glue code. If you’re exploring integration architecture, you’ll sooner or later stumble upon RESTful web services in Apex. In plain terms: REST lets outside systems reach into Salesforce to fetch or push data using standard HTTP verbs like GET, POST, PATCH, and DELETE. The beauty? A clean, scalable contract for data exchange that plays nicely with modern cloud apps, mobile apps, and third-party platforms.

A gentle nudge toward the right entry point

Think of building a REST API in Salesforce as setting up a door that others can walk through. The door has a sign that says which room you’re entering and what you can do once you’re inside. In Salesforce, that door is created by annotating a class so Salesforce knows this class is a RESTful web service. The core annotation that makes this happen is @RestResource. It designates the Apex class as a REST resource and defines the URL pattern that external systems will use to reach it. Inside that class, methods can be mapped to HTTP methods such as GET or POST, letting you tailor the behavior to the needs of each operation.

Let’s map out the anatomy of a RESTful entry point

  • The gateway: @RestResource

This annotation is the beacon. It tells Salesforce, “Hey, this is a RESTful resource.” It configures things like the URL path segment, the description, and what kind of response formats you’ll deliver (typically JSON, sometimes XML). Without this gateway, the inner methods would have no clean, discoverable external surface.

  • The interior: @HttpGet, @HttpPost, @HttpPatch, @HttpDelete

These method-level annotations specify what HTTP verbs a given Apex method responds to. They’re the musical notes inside the REST orchestra, giving you precise control over which operations are allowed, and under what circumstances.

  • The format and security layer

REST APIs in Salesforce usually exchange JSON, with careful attention to serialization and deserialization. You’ll often see a class that methods parse incoming JSON into Apex objects, and then build a response object that Salesforce converts back into JSON. Security comes into play with session-based authentication or OAuth tokens, ensuring only trusted apps can call your endpoints.

A practical look at the workflow

Imagine you’re building an integration that needs to expose account data to an external marketing platform. Here’s a simple, high-level flow you might design:

  • You create a new Apex class annotated with @RestResource, specifying a clear, predictable URL path, like /services/apexrest/MarketingAccounts.

  • Inside that class, you define a method annotated with @HttpGet to fetch account records based on certain query parameters (for example, a status filter or a date window).

  • You might add another method annotated with @HttpPost to create a new account in Salesforce, accepting a JSON payload with the account details.

  • You’ll ensure proper error handling, returning meaningful HTTP status codes and a tidy JSON payload that the external system can parse easily.

  • You implement security checks, possibly validating a token supplied by the caller, to guard access to sensitive data.

A few practical tips that tend to save a lot of headaches

  • Start with a clean URL design

A consistent URL scheme is essential. For instance, if you expose a resource at /services/apexrest/MarketingAccounts, then nested routes like /{id} or /{id}/contacts can feel intuitive. Predictable paths reduce friction for developers consuming the API.

  • Keep methods focused

Each Apex method should have a single responsibility corresponding to its HTTP verb. GET should fetch, POST should create, PATCH should update, and DELETE should remove. Yes, you might be tempted to cram multiple behaviors into one method, but modularity pays off in maintainability.

  • Model your data thoughtfully

Before you code, sketch a compact data contract. What do you send in, what comes back out? A well-defined data shape minimizes the chance of misinterpretation and makes it easier for external systems to integrate.

  • Make error messages signal-friendly

Return structured error responses with a clear message, an error code, and, when appropriate, guidance for remediation. A good error narrative can save downstream developers a lot of debugging time.

  • Document what you expose

Even if you’re not writing a formal API spec, a concise doc that lists endpoints, supported HTTP methods, parameters, and sample requests/responses goes a long way. It’s a friendly map for anyone who might use your service.

Common misconceptions that trips people up

  • The belief that REST is a big, scary thing

REST isn’t a mystic ritual. It’s a pattern that uses standard HTTP semantics. The heavy lifting comes from good organization, solid error handling, and clean data contracts—not from clever tricks.

  • Thinking @HttpGet and @HttpPost are standalone magic

They’re powerful, yes, but they work inside a class that’s already marked with @RestResource. The annotation at the class level sets the stage; the method-level annotations define the actions.

  • Assuming REST and SOAP are enemies

REST and SOAP are different approaches to web services. In Salesforce, SOAP remains relevant for some enterprise scenarios, but REST shines when you want lightweight, easy-to-consume APIs that play nicely with web and mobile apps. It’s not about picking sides; it’s about picking the right tool for the job.

A quick mental map for the engineer’s toolkit

  • Start with a clear REST surface

Decide what resources you’ll expose and how clients will navigate them. This is your user-facing API surface, so clarity wins.

  • Annotate with purpose

Use @RestResource to declare the resource, and @HttpGet/@HttpPost (and others) to tie actions to HTTP verbs. Keep the implementation tight and readable.

  • Handle authentication gracefully

OAuth tokens or session-based checks are common paths. The key is to verify identity early and fail safely if something looks off.

  • Test with real-world clients

Tools like Postman or REST clients in your favorite editor help you visualize how your service behaves across different scenarios. It’s easier to iterate when you test against the actual surface.

  • Observe and refine

Monitor usage patterns and error rates. If you find certain endpoints are underused or error-prone, adjust the design. REST is as much about evolution as it is about construction.

Digressions that feel natural, but come back home

If you’ve ever grilled a perfect steak, you know the art of timing matters. Too long on the heat, and it’s dry; too short, and it’s unripe. The same principle applies to API design. A REST endpoint shouldn’t linger in the realm of “nice-to-have.” It should be crisp, predictable, and reliable. Think about what your consumer apps need to do and craft a surface that makes those actions effortless. After all, an API that’s easy to use invites more robust integrations, fewer back-and-forths, and happier developers.

A few industry-flavored metaphors to keep in your pocket

  • The doorbell and the doormat

@RestResource is the doorbell; the concrete path you set is the doormat leading visitors to the rooms inside. The inside rooms are your methods, ready to offer services when the right button is pressed.

  • The librarian and the catalog

The REST surface is a catalog of services. Each resource is a shelf, each method a labeled drawer. If the catalog is chaotic, no one finds what they’re after. A tidy, well-documented catalog saves everyone time.

  • The bridge, not the fortress

A REST API should open channels for safe, authorized data exchange. It’s a bridge, not a moat. Design with openness in mind, but don’t skip on security.

What success looks like in real life

A well-crafted REST API in Salesforce feels seamless to those who consume it. The external systems confidently fetch data, push updates, and even trigger workflows, all without wrestling with messy integration glue. The Apex code stays readable, tests stay meaningful, and the whole stack behaves like a well-rehearsed orchestra. You have a clear URL structure, predictable behavior across HTTP methods, and robust error handling that speaks the same language as client developers.

Final thought: start with the simplest, most honest surface

If you’re stepping into REST in Salesforce, begin with the simplest resource that makes sense for your scenario. Annotate the class with @RestResource, map a couple of essential endpoints to GET and POST, and build out from there as needs arise. The goal isn’t to pile on features—it's to deliver a clean, dependable doorway that other systems can rely on confidently.

As you continue to explore, you’ll find that REST isn’t just a tech pattern. It’s a practical philosophy for interoperability, a way to thread Salesforce into the broader ecosystem with clarity and grace. And when that door opens smoothly, you’ll sense the quiet satisfaction of systems talking to each other the way they’re supposed to—efficient, predictable, and just a little bit elegant.