Platform
Apex REST API: design a maintainable Salesforce endpoint
Choose Apex REST only when standard APIs do not fit, then define permissions, idempotency, versioning and error responses.
A custom endpoint creates an interface your team must operate and maintain. Check the standard APIs before adding Apex REST.
Salesforce’s Apex REST design guidance compares custom endpoints with REST and Composite APIs. Standard record operations or a supported composite transaction may already satisfy the requirement. Use custom Apex when you need a specific business operation that those interfaces don’t express adequately.
Define the operation before the URL
Write down the input, authorized caller, affected records and confirmed result. “Approve a request” is a business operation; “update any object using this JSON” is a broad capability that requires much more control.
Choose a versioned path, bounded payload and explicit schema. Distinguish required values from optional ones, and decide how unknown fields are handled. Keep internal class names and implementation details out of the public contract.
Apex REST uses @RestResource with HTTP method annotations. A successful response should describe the actual outcome: created, updated, rejected or accepted for later work. Returning HTTP success after swallowing a DML exception gives the caller the wrong state.
Make authorization explicit
Authentication identifies the caller; it doesn’t establish permission for every record or action. Review class access, object and field permissions, record sharing and the business authorization rule.
The secure Apex reference documents API-version differences in default execution behavior. Use explicit declarations and operation modes appropriate to the contract, and test as the intended integration user. Don’t rely on an administrator-only smoke test.
Allow-list writable fields. A generic deserialization-to-DML path can accept changes the business operation never intended to expose. Validate record references in the destination org and avoid echoing unrestricted payloads in errors.
Design retries before enabling writes
Give consequential create or action requests a stable external operation identifier. Define what happens when the same identifier arrives twice and when the same identifier arrives with a different payload.
Consider a timeout after the transaction commits. The caller may see no response even though the write succeeded. A retry must be able to retrieve or reconcile the original result rather than create a duplicate.
For asynchronous work, return an operation reference and provide a status mechanism. Enqueuing a job is not confirmation that the business action completed.
Test the contract from the caller’s perspective
Unit tests can construct RestRequest and RestResponse in RestContext and invoke the handler. Add integration testing in an approved non-production environment for authentication, permissions and HTTP behavior.
Cover malformed input, unauthorized fields, inaccessible records, duplicate requests and a validation failure. Assert response status, response body and resulting database state. Include bulk or payload-boundary cases when the endpoint accepts collections.
For Salesforce and ServiceNow integration, agree which system owns the ticket and which transitions may cross the boundary. The ServiceNow implementation page describes the discovery work behind that contract. A reliable endpoint makes ownership and recovery visible before the first production retry.