Step-by-Step Guide to Creating a Custom Action for Agent Using Apex REST

When Your Agent Needs Data from Outside Salesforce

Agentforce supports a range of action types out of the box — standard actions, Flow actions, and custom Apex actions etc. For most tasks within Salesforce, these cover a lot of ground.

But some use cases require data that lives outside Salesforce entirely. When a support agent needs to know the weather at a customer's location during a live call, that data doesn't exist in any Salesforce record. The agent needs to reach out to an external service to get it — and that is where Apex REST comes in.

Apex REST lets you write custom callout logic in Apex, expose it as a REST endpoint, and wire it into Agentforce as a Custom Action. It is particularly useful when you already have existing Apex REST APIs in your org that you want to expose to Agentforce without rewriting them, or when the integration pattern suits a REST-based design.

This guide uses WeatherAPI as a practical, easy-to-follow example — but the pattern applies to any external system you can reach from Salesforce.

By the end of this guide, your Agentforce Agent will be able to retrieve the current weather at any Contact's location — directly within Salesforce, without switching apps or leaving the record.


How This Implementation Works

There are four layers to this implementation, each with a distinct job.

1. External Credential and Named Credential — Secure Authentication

The WeatherAPI key never appears in code. Instead, it is stored as an Authentication Parameter under a Named Principal in an External Credential. The Named Credential stores the WeatherAPI base URL and references the External Credential for authentication. From Apex, the callout is made by referencing the Named Credential — the platform handles the rest.

This is the right pattern for any external API integration in Salesforce. Credentials stay in Setup, out of code, and can be rotated without a deployment.

2. Apex REST Class — The Callout Logic

The WeatherAPIService class is annotated with @RestResource and @HttpGet, which exposes it as a REST endpoint. When invoked, it accepts a Contact ID, queries the Contact's full mailing address — builds the address into a query string, and calls the WeatherAPI endpoint. The response is parsed and returned containing the Contact's details alongside weather data: temperature in both Celsius and Fahrenheit.

3. OpenAPI Document and API Catalog — The Bridge to Agentforce

This is where this guide's approach differs from the @InvocableMethod pattern you may have seen in other guides. Rather than annotating a method directly for Agentforce, this guide uses the Apex REST class combined with an OpenAPI document — the path Salesforce introduced at TDX '25.

OpenAPI is a standard format for describing RESTful APIs. Think of it as a blueprint that tells both humans and systems what an API does, what it accepts, and what it returns — without reading the source code.

The Salesforce Extension option in VS Code

SFDX: Create OpenAPI Document from this Class

generates two files from your Apex REST class: a .yaml OpenAPI specification and an .externalServiceRegistration-meta.xml metadata file. Deploying these registers the API in Salesforce's API Catalog, making it available as a Custom Agent Action of type Apex REST (Beta).

The two paths to creating a Custom Action from Apex are:

  1. Invocable Method - @InvocableMethod - Directly available in Agent Builder

  2. Apex REST + OpenAPI - @RestResource - Registered via API Catalog → Apex REST (Beta) action type

Both are valid. The Apex REST path is particularly useful when you have existing Apex REST APIs you want to expose to Agentforce without rewriting them as invocable methods.

4. Custom Agent Action — Wiring It to the Agent

The Custom Agent Action is created from the registered API in Agentforce Assets, making it reusable across multiple agents.

Once created, the action is added to the Agent's topic and the agent is activated.


Step-by-Step Guide

In this step-by-step guide (with screenshots) we’ll cover:

  1. Sign up and set up an Agentforce Developer Edition org

  2. Create a new Employee Agent in Agentforce Builder

  3. Register for a free WeatherAPI account and note the API key

  4. Create an External Credential with the API key stored as an Authentication Parameter

  5. Create a Named Credential with the WeatherAPI base URL

  6. Configure a Permission Set for External Credential access

  7. Create the WeatherAPIService Apex REST class (the code is provided in the Academy)

  8. Generate an OpenAPI document using VS Code and register it in the API Catalog

  9. Create a Custom Agent Action from the registered API using Agentforce Assets

  10. Add the Custom Action to the Agent

  11. Test the agent



Summary

Apex REST as an agent action path is underused — partly because the @InvocableMethod approach is simpler for greenfield development, and partly because the OpenAPI generation tooling is relatively new. But for teams with existing Apex REST APIs, or for integrations that involve external systems, this pattern is clean and worth knowing.

The setup has more moving parts than a simple invocable method — credentials, OpenAPI generation, API Catalog registration — but each part has a clear job. Once you have built it once, the same pattern applies to any external API callout you need to wire into an agent.

The example in this guide uses WeatherAPI, but the architecture is identical regardless of which external service you are integrating with.


Agentforce Advisory, Training & Implementation Services

If you’d like help going deeper with Agentforce beyond this guide, I offer the following:

  • Self-Paced Training – #Mastering Agentforce

    A structured, hands-on course covering Agentforce concepts, Prompt Templates, Agents, Actions, and real-world implementation patterns.

  • Live Online & Onsite Agentforce Training

    Instructor-led training for Salesforce teams, partners, and customer organizations, tailored to real use cases and practical scenarios.

  • Agentforce Advisory & Implementation Services

    Strategic advisory and hands-on implementation support to help you design, build, and scale Agentforce solutions with confidence.

For training, advisory, or implementation discussions, please reach out via the Contact page, where you can submit a form or schedule a conversation.


References & Useful URLs



Got a takeaway from this guide, or have you implemented it in your org? I'd love to hear what you built or what you learned — drop a comment below. 👇