> For the complete documentation index, see [llms.txt](https://docs.getmesa.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.getmesa.com/tools/api.md).

# API

The **API** tool allows you to connect MESA with internal services or any publicly-available API.

{% hint style="info" %}
If you’re looking to trigger your workflow from an API we recommend using a [Webhook](https://docs.getmesa.com/tools/webhook) if your API supports webhooks. Otherwise, you can use a [Schedule](https://docs.getmesa.com/tools/schedule) trigger and an API action in later steps.
{% endhint %}

### Authentication Methods <a href="#actions" id="actions"></a>

MESA offers [4 different built-in Actions](/workflow-builder/triggers.md) that you can utilize to connect to an API.

* [No Authentication](#no-auth)
* [API Key](#api-key)
* [OAuth 2.0](#oauth-2)
* [Basic Auth](#basic-auth)

<figure><img src="/files/jvXo20V5ZqFwthvYce4W" alt="Screenshot of the API tool action setup in the MESA workflow builder showing the four built-in authentication methods. Spotlight the authentication method selector listing No Authentication, API Key, OAuth 2.0, and Basic Auth."><figcaption></figcaption></figure>

#### No Authentication <a href="#no-auth" id="no-auth"></a>

Used for interacting with an API that does not require any authentication and provides public API access. To configure the **No Authentication** Action, please refer to the [Configuration information below.](#no-auth-example)

#### API Key <a href="#api-key" id="api-key"></a>

One of the most common ways to connect to an API.

<figure><img src="/files/vAF75ofvCmr6tRagVrMY" alt="Screenshot of the API Key connection setup in MESA, adding a new API Key credential. Spotlight the Key and Value fields."><figcaption></figcaption></figure>

**Key:** The name of your API key. Your service's API documentation will specify this.

**Value:** The value of your API Key that you get from the service's dashboard.

MESA will pass down the newly created connection as a key value pair in the header object. If the service expects the API key to be a query string parameter, use [API's No Authentication](#no-auth) step. Here is an example:

<figure><img src="/files/Y3Ab1AR3yPTN6wJtjpTT" alt="Screenshot of the MESA Logs showing an API Key request passed as a key value pair in the header object. Spotlight the header object containing the API key."><figcaption></figcaption></figure>

To configure the API Key Action, please refer to the [Configuration information below.](#configuring)

#### OAuth 2.0 <a href="#oauth-2" id="oauth-2"></a>

One of the most common ways to connect to an API but requires a more complex set-up. This requires creating an app in your service and entering the information into MESA to create a [connection](https://docs.getmesa.com/going-further/credentials). When creating an app in your service, you must use this URL as the callback URL: <https://app.getmesa.com/apps/mesa/oauth/redirect-token>

<figure><img src="/files/3VjqBRPmBN3iDT39pqbO" alt="Screenshot of the OAuth 2.0 connection setup in MESA where you enter the app details from your service. Spotlight the callback URL field."><figcaption></figcaption></figure>

If your service requires MESA to make a POST request to refresh a token, you can use the Use POST for token refresh setting in the [More options](https://docs.getmesa.com/workflow-builder/fields).

<figure><img src="/files/RCXalfcQy9tUeYBQ3yTE" alt="Screenshot of the API step in the MESA workflow builder with the More options section expanded. Spotlight the More options link."><figcaption></figcaption></figure>

<figure><img src="/files/HplVEWeHgZgHbQkuXLgU" alt="Screenshot of the API step More options in MESA showing the token refresh setting. Spotlight the Use POST for token refresh checkbox."><figcaption></figcaption></figure>

By default, MESA will refresh tokens by using query parameters. If you mark the checkbox, MESA will use POST content to refresh the content.

#### Basic Auth <a href="#basic-auth" id="basic-auth"></a>

Basic Authentication is a simpler method of authentication that involves a username and password. For example, a Shopify private app. You will need to create a [Connection](/going-further/credentials.md).

<figure><img src="/files/zYXDdJHuYkyzEW2SZzNl" alt="Screenshot of the Basic Auth connection setup for the API tool in MESA. Spotlight the username and password fields."><figcaption></figcaption></figure>

## Configure <a href="#configuring" id="configuring"></a>

In the API actions, you will find the following fields:

**Method:** GET, POST, DELETE, PUT, PATCH (for a summary, read [this article](https://www.w3schools.com/tags/ref_httpmethods.asp))

**URL:** Endpoint of a service, for example, <https://httpbin.org/anything>. If you are sending URL parameters, include these here. **Important:** Make sure to include "https\://" in the url.

### Advanced Settings (found via [More Options](https://docs.getmesa.com/workflow-builder/fields#additional-fields))

**Content Type:** Used to tell the client about the original [media type](https://developer.mozilla.org/en-US/docs/Glossary/MIME_type) of the resource

**Request Body:** Will contain the information that you want to send to the service - used by POST, PUT, PATCH, and DELETE requests

**Headers:** Lets the client & server pass additional information with an HTTP request or response, for example, authentication tokens or the format to return. More information can be found [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers).

**Querystring parameters:** A part of a uniform resource locator (URL) that assigns values to a specified parameter(s). `Example: https://example.com/over/there?name=ferret`

**Specify a custom Request Body:** This allows you to specify text or a variable for the request body. For instance, you could specify a variable that refers to data from a previous step. The text specified here will be sent as the Request Body instead of anything specified under the **Request Body** above.

## Going Further <a href="#examples" id="examples"></a>

### Examples <a href="#no-auth-example" id="no-auth-example"></a>

#### API: No Authentication

When a product is updated, we will make a POST request to the following endpoint: <https://httpbin.org/anything>

Once a workflow runs and the request has been sent, httpbin will return the data (product title, product handle, and price) that we sent to the endpoint.

<figure><img src="/files/tMT3G3tYV7LF1va7Pi85" alt="Screenshot of the API No Authentication action in the MESA workflow builder configured to make a POST request to the httpbin endpoint. Spotlight the Method and URL fields."><figcaption></figcaption></figure>

<figure><img src="/files/Bmw5nmrMo5WsUWfOf88f" alt="Screenshot of the API No Authentication action in MESA showing the product data (title, handle, and price) configured to send to the httpbin endpoint. Spotlight the Request Body fields."><figcaption></figcaption></figure>

After updating a product and viewing the [Logs](/workflow-activity/logs.md), this was the Request:

![Screenshot of the MESA Logs after updating a product showing the API Request that was sent to httpbin. Spotlight the Request body.](/files/Han7QQ8o3VZXBcARLcUs)

This was the Response:

![Screenshot of the MESA Logs after updating a product showing the API Response returned by httpbin. Spotlight the Response body.](/files/7tYSVGL2M56ASDSwx781)

#### Use data obtained from an API <a href="#data" id="data"></a>

In this example, we will interact with [OpenWeatherMap's API](https://openweathermap.org/) and update a Shopify order's Notes with the current temperature of the customer's zip code once an order is created.

To utilize their API and obtain an API Key, you will need an [account](https://home.openweathermap.org/users/sign_up).

1\. You can start building your own workflows by clicking the New Workflow button on the right-hand side of the My workflows page.

<figure><img src="/files/uGtIqIEfn73R4O4XUEe9" alt="Screenshot of the My workflows page in MESA. Spotlight the New Workflow button on the right-hand side."><figcaption></figcaption></figure>

<figure><img src="/files/cSWjHZQ4QbzSL9voVeja" alt="Screenshot of the new workflow creation screen in MESA after clicking New Workflow. Spotlight the trigger selection to add the Shopify Order Created trigger."><figcaption></figcaption></figure>

2\. Select the Shopify Order Created trigger. Next, click on Add Step > Actions, search for "API", select the API tool, and then the [No Authentication](#no-auth) action.

3\. For the Method and URL, you will need to decide on your preferred endpoint. [Here is an example.](https://openweathermap.org/current)

<figure><img src="/files/q0MW9iX1prXtR6qLBKkY" alt="Screenshot of the API No Authentication action in the MESA workflow builder for the OpenWeatherMap example. Spotlight the Method and URL fields."><figcaption></figcaption></figure>

<figure><img src="/files/mv6ArakyrWZbxM0hmi2T" alt="Screenshot of the API No Authentication action in MESA showing the Querystring parameters for the OpenWeatherMap request. Spotlight the zip and appid parameter fields."><figcaption></figcaption></figure>

4\. For the Querystring parameters, you will need to add the parameters (zip and appid).

* zip (Required): You can use [MESA Variables](/workflow-builder/fields/variables.md) to use the Shopify Order's zipcode and country code. For this example, the variable is (comma included): {{shopify\_order.shipping\_address.zip}}, {{shopify\_order.shipping\_address.country\_code}}
* appid (Required): Your unique API key, and you can find it on your account page under the [API key tab](https://home.openweathermap.org/api_keys).
* units: Optional parameter to specify the temperature units.

5\. Click on the **+** symbol below that step and select the Shopify Update Order Notes Action. This action will only add the preferred text and will not override any existing order notes on the order.

6\. In order to view the information sent by OpenWeatherMap, you will need to enable the [Logging, debug mode logging](/workflow-activity/logs.md), and test your workflow. To learn how to test your workflow, [click here](/workflow-builder/testing.md).

7\. After a test, you can view the Variables Available to view the response from OpenWeatherMap with the debug logging enabled.

<figure><img src="/files/u7Hcl3p1j0Kh6qYBZdUl" alt="Screenshot of the API step in MESA after a test run showing the OpenWeatherMap response. Spotlight the Variables Available section."><figcaption></figcaption></figure>

8\. By viewing the Variables Available, you can figure out the variable to use for the later steps in the workflow.

9\. In the Shopify Update Order Notes Action, fill out the following:

* **Shopify Order ID**: Select the Shopify Order Created ID [variable](https://docs.getmesa.com/workflow-builder/fields/variables).
* **Notes:** If you want to pass the current temperature data down in your workflow, you can use this variable: {{api.main.temp}}

To create a custom variable, click on the plus sign icon next in Settings for that particular API step. Then, click on the Copy key.

<figure><img src="/files/T4gnwEvgYi2N4q274D7c" alt="Screenshot of the API step Settings in MESA for creating a custom variable. Spotlight the Copy key button next to the plus sign icon."><figcaption></figcaption></figure>

In one of the fields, you can type `{{` and then paste the copied value. Next, you can manually type in the data that you'd like to send. End the variable with `}}`

An example of a custom variable is: `{{api.feels_like}}`

Here is an example of updating the order notes with the current temperature.

<figure><img src="/files/Ap4jduhinoNDaJGTw13h" alt="Screenshot of the Shopify Update Order Notes action in MESA populated with the current temperature variable. Spotlight the Notes field containing the api.main.temp variable."><figcaption></figcaption></figure>

#### Send nested data to an API <a href="#nested-data" id="nested-data"></a>

To send nested data to an API, you can use dot notation. Here is an example of a nested data structure, with the data nested under **data**:

```
{
  "data": {
    "order_id": "2975040733210",
    "total_price": "403.00"
  }
}
```

Here is how you would set this up in MESA.

<figure><img src="/files/gEfuDEYbO1SHiiiywM13" alt="Screenshot of the API action in the MESA workflow builder configured to send nested data using dot notation. Spotlight the Request Body fields showing the data.order_id and data.total_price keys."><figcaption></figcaption></figure>
