Avochato API Callouts via Apex Code

Easily send messages and use other Avochato API endpoints with just a few lines of Apex code.

This post outlines examples of using the AvochatoApiClient in Apex code to easily leverage the Avochato API at www.avochato.com/docs

Avochato in Apex

The Avochato for Salesforce has built-in helper Apex classes to easily send messages and use other Avochato API endpoints with just a few lines of Apex code.

In order to enable Avochato API callouts in Apex code, first setup your Avochato API credentials in Salesforce by following the Avochato API Credentials Datatable post.

Basics: Send Avochato SMS Message

Let’s examine the code below to see a basic example of the AvochatoApiClient in action:

// Uses default settings and loads client based default Avochato_Credential__mdt settings
Avochato.AvochatoApiClient api = new Avochato.AvochatoApiClient();

// The API callout type you want to make

// Adding parameters based on the Avochato API documentation
api.addParam('phone', toPhone);
api.addParam('message', message);
if (String.isNotBlank(fromPhone)) api.addParam('from', fromPhone);
if (String.isNotBlank(tags)) api.addParam('tags', tags);


The Avochato.AvochatoApiClient is the main HTTP client that grabs the proper Avochato API credentials that were provided in the Avochato Setup → Advanced tab in Salesforce, as well as handles the building of the whole HTTP request internally.

addUrlPart('message') is building the URL items as necessary for the callout. In our case of an Avochato Send Message callout, message is the only URL part needed aside from the payload itself.

addParam('key', value) adds the particular key to the JSON payload with the specified value. Add all the required parameters for the particular API callout and any additional parameters along the way

post() sends the actual HTTP request to make the API callout.

Advanced: Send Avochato SMS based on specific API credentials

If you want to specify a particular API credential to use, we can use a different constructor for the AvochatoApiClient:

AvochatoApiClient(String avoAcct, String avoUsr, Id sfUser)

The avoAcct is the Avochato Inbox that was specified in the Avochato API Credential Datatable. If you have one API credential per Avochato Inbox, then this is a great way to pull that particular API credential for sending the message:

Avochato.AvochatoApiClient api = new Avochato.AvochatoApiClient('Oregon Office', null, null);

If there’s also a particular Avochato user that you’d like to send the message on behalf of who is in that “Oregon Office” inbox, and assuming you created API credentials for that user, you can additionally specify the avoUsr parameter (usually the first and last name of the Avochato User):

Avochato.AvochatoApiClient api = new Avochato.AvochatoApiClient('Oregon Office', 'Joe Smith', null);

When adding Avochato API Credentials to the datatable within the Avochato Setup → Advanced tab, we also save the Salesforce User ID as part of that credential. You can just specify that User ID to select that API credential for use:

Avochato.AvochatoApiClient api = new Avochato.AvochatoApiClient(null, null, UserInfo.getUserId());

Did this answer your question?