# API Connector

## Import data from API

Application Programming Interface or API is an important data source that Acho can support. You can reference the following steps to build a connection with your API. If you need any help to set up the API connection, please [contact us](https://docs.acho.io/master#how-to-contact-us).

1\. Go to the **Resources** page and click the **Add Resource** button.

2\. Select **API**.

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-Mhn_eaghZohJ7dc9upD%2F-MhncYCq11pEu-fKtGQr%2Fimage.png?alt=media\&token=b577d1f1-c9f3-44b5-9a29-cd75f5d841ab)

3\. Configure your API. You can click the **Test** button to see whether to get the response normally. (Note: please see [here](#how-to-configure-your-api-connection) to learn how to set up each field)

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FXsyooRmDuPAh9fx545di%2Fimage.png?alt=media\&token=17917ef5-73fc-486e-8974-674b90792fdc)

5\. Click **Connect**. You will see a ![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FBdjPs5XIfHwGsorv7Vzl%2Fimage.png?alt=media\&token=21f10b78-d430-42fc-9075-51d52c93fac4) icon next to your API resource. It may take a few minutes to several hours depending on the data size. Once your data is ready, you will receive a notification email.

## Configure your API connection

### **1. Resource Name**&#x20;

The displayed name of your API resource on the resource page.

### **2. URL**&#x20;

It's the API URL path to connect with your apps or integrations. Acho supports any APIs that follow REST conventions. For example,`https://api.unibit.ai/v2/stock/historical?tickers=AAPL&accessKey=demo&dataType=json`**.**

### **3. Parameters**&#x20;

API parameters are the optional key-value pairs used for determining the details of your API. Generally, you can specify them after the question mark (`?`) in your API. If there are multiple parameters, they will be appended together with an `&` sign. Take the following API as an example. There are three parameters:

* tickers = AAPL
* accessKey = demo
* dataType = json

![Parameters example](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F3D7S6bisA3UyyDHM9QIO%2Fimage.png?alt=media\&token=aac52734-9264-458e-9ea5-d07b75e3b090)

Acho supports two ways to specify your parameters.&#x20;

#### **1. Specify parameters in your URL**&#x20;

You can define parameters inside the URL field directly. Remember to specify them after a question mark and use "&" to concatenate all parameters. However, if you use {} to specify an embedded parameter, "{}", in your URL, you can only specify other parameters in [the Parameters tab](#2.-specify-parameters-in-the-parameters-tab).

![API URL Field](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FqT6r9AKSoxP6TiG3MFoj%2Fimage.png?alt=media\&token=9fa7812f-7912-4dd7-8656-1e0fc6a16a4f)

#### **2. Specify parameters in the Parameters tab**

Another way is to list each key and value in the Parameters tab as shown below.&#x20;

![Parameters Tab](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FpWDKFXDlHhY3er6mK00M%2Fimage.png?alt=media\&token=edb9dea7-a3da-4f07-8786-a59067d7d2d0)

### **4. Authorization**

The input under this section is highly defined by the host of the API you are trying to connect with. Always make sure to check the documentation of the API you would like to connect to.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FO2mJ4ngGfWdd1AYyfcrf%2Fimage.png?alt=media&#x26;token=16134413-5340-4fe4-97ba-d967e43f6323" alt=""><figcaption></figcaption></figure>

Acho supports four types of authorization to validate API access.

* **None (default)**: If your API doesn't require authorization, you can select **`None`**.&#x20;
* **Basic Auth**: It's the most straightforward way for authorization. It usually asks for the username or email and password.
* **Bearer Token**: It's the most common method these days. For most software, you can generate the token under the Settings or Integrations tab of the website.
* **OAuth Client**: If the API requires an OAuth authorization, an OAuth Client must be created. Usually, the software will ask you to create a custom app to access the account, and then you will need to fill in the following fields using the info of the custom app. If you have any questions about this, feel free to message us at <https://drift.me/acho> or email us at <contact@acho.io>
* **HMAC**: Used to verify that a request is coming from an expected source and that the request has not been tampered with in transit. It’s a standard protocol but with custom setups for different applications. To use HMAC as your Authorization method, please message us at <https://drift.me/acho> or email us at <contact@acho.io>

### **4.1 OAuth Client**

1. When creating an API Resource, you can select OAuth Client for the Authorization Type.<br>

   <figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FbDNBNeRZkzCVb8KjCaH7%2Fimage.png?alt=media&#x26;token=0539f64d-693f-48b7-9429-4fb380ad306d" alt=""><figcaption></figcaption></figure>
2. Click + if you don't have one.
3. Enter your info, please name it well, then click Add.\
   ![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FVwkjpTzZJgJquiFcotsq%2Fimage.png?alt=media\&token=5f0844f7-37a4-436f-b58e-5b1c1fdf8d71)
4. A new tab will be opened, if it's a login page, you can log in, and after you see an object (including an access token), you can close that tab. You may see the object immediately, which means you are logged in already, you can just close the tab.
5. Click Next.
6. Select the client you just created.\
   ![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FBfNJe93u8fOakyHxn55H%2Fimage.png?alt=media\&token=505fb073-50ed-4fde-8c2d-e0af3571434a)
7. You are all set, you can test your API now.

### **5. Headers** (Optional)

The headers are additional information included in the API requests. Similar to parameters, the headers also consist of key-value pairs. You will need to check the API documentation to find out the required and optional headers. The most common inputs for headers are&#x20;

1. Content-Type: Specifies the format of the data being sent or received, such as JSON, XML, or form data
2. Authorization: We will insert your input from the Authorization section to the header. The format will be something like “Authorization: Bearer your\_bearer\_token\_here”. Normally you don’t need to specify this by yourself if you already have filled in the Authorization section.
3. Customized field: For example, Shopify requires a header called X-Shopify-Access-Token and the Access Token of the custom app as the value of it.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FAbFTC7tgSvUcjoKrauD5%2Fimage.png?alt=media&#x26;token=cd6f06ba-fb6c-456c-a467-dedab7be05e7" alt=""><figcaption><p>Shopify API Sample</p></figcaption></figure>

### **6. Body**

The body is only applicable to POST requests. It refers to the main content or payload of a request. It contains the data that is being sent from the client to the server.

For example, this is a sample body to create a product on an e-commerce website.

<figure><img src="https://lh3.googleusercontent.com/G4n7SfiHTmRxdnOHlL0pe-JzU8-XFJB_NasHsIA_gGM6IiE7srY9A2zsphPUGrPY6QVHrGFnbP-S03_JRkrIBfIhpoM9t3UvtpKo88WaQx5LEbToNKw6a2BJci4obcZU9CJhwFOXx2DQwq9vjrC0yC0" alt=""><figcaption></figcaption></figure>

### **7. Data Format**

Currently, we support 3 formats, CSV, JSON, and XML. JSON is the most common format, as almost all APIs support it. The JSON Path is an optional field to define the specific data you want to get from the response.

#### **JSON (default)**&#x20;

Example:

```
https://api.unibit.ai/v2/stock/historical?tickers=AAPL&accessKey=demo&dataType=json
```

If you pass it into the URL field and click **Test**, you can get the following response.

![API data in the JSON format](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FP5fH1TCdrz9LSlaniq5D%2Fimage.png?alt=media\&token=1baa9710-8232-4800-a9f9-e29bc4814424)

#### **JSON  - JSON Path (optional)**

If your data is stored in a nested JSON object, you can define the path to tell the system where to access it. In the above example, the raw data is stored inside the "AAPL" key and the `AAPL` key is stored in the `result_data` key. To access the raw data, the path can be defined as `result_data.AAPL`.

![JSON - JSON Path Example](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FCZ81UMIu0Yg1d8HHHtri%2Fimage.png?alt=media\&token=28f534c8-6bf9-43cb-886b-93407fd7c4f3)

#### **CSV**

A CSV file refers to a "comma-separated values" file uses a comma to separate values. If your API returns a downloadable CSV file, you have to specify **`CSV`** in **Data Format**. Thus, the system will parse your data correctly. Here is an example API:&#x20;

```
https://api.unibit.ai/v2/stock/historical?tickers=AAPL&accessKey=demo&dataType=csv
```

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2Fy5p02mDlLFfv1DFjdbSj%2Fimage.png?alt=media\&token=b9f59fab-9e88-4f70-9a84-a9534a60f5fc)

#### **XML**

XML is a markup language similar to HTML, but without predefined tags to use. Here is an XML response example:

```
https://stageappsp.smashfly.com/contactmanagerservice/v2/ContactManagerRestService.svc/help/operations/SaveContact
```

You can get the response as shown below

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FPMAqPw1wtjx0mRqsTpA0%2Fimage.png?alt=media\&token=39fbb9a6-ab7a-466a-b35d-81cd5ed76b90)

**XML - JSON Path**

Acho turns the XML response into a JSON format so that you can define JSON path to access the specific data you need easier. For example, if you only need `body` data within the `html` key, just specify `html.body` in **JSON Path**.&#x20;

![XML - JSON Path example](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FzPvGJhbyss7rJJfZNbqo%2Fimage.png?alt=media\&token=f8db1c00-9d27-4a02-8429-d35b72458f73)

### **8. Multiple Requests**

Retrieving all records in a single request is impossible when you have lots of data to get. By utilizing the Multiple Request setting, you can set the API connector to run multiple times based on the API requirements. Usually, there are three common methods: **Iteration**, **Recursive** and **Loop**.

<div align="left"><figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F9wzyIPjnQ9vg5P3dWl3V%2Fimage.png?alt=media&#x26;token=a4c26758-18d2-40fa-86ce-2a854c4abdea" alt="" width="563"><figcaption></figcaption></figure></div>

### **Method 1: Iteration Requests (Pagination):**&#x20;

Iteration works the same as pagination. This method splits data into several batches based on the row number and only retrieves one batch of the data at a time until there is no data that can be retrieved.&#x20;

For example, if the original dataset has 3000 records and we retrieve 1000 rows at a time, the system will call the API three times to get all data.&#x20;

![The Mechanism of Iteration Requests](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FpzYaFk6jFS2wahM4CMaY%2Fimage.png?alt=media\&token=4fadc919-468f-49fb-9cfc-8185e7480aa4)

Options:

* **Request Limit**: The maximum number of records to retrieve at a time. Most of the APIs named this parameter `limit` or `page_size`.&#x20;
* **Start Parameter Key**: A parameter to decide where to start to retrieve data. Most of the APIs named this parameter `start` or `page`.  The **Start** for most APIs is either 0 or 1. If the pagination is by page, the **Step** is usually 1 (just like flipping pages). If the pagination is by number, the **Step** is usually the same as the Request Limit number.&#x20;

**Two most common examples:**

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FbvAdAnnadPDzAd8unwvc%2Fimage.png?alt=media&#x26;token=a24a4ba1-ee1c-432c-b78c-26169b9af52b" alt=""><figcaption><p>Pagination by number</p></figcaption></figure>

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FmQxdpEWTDS9joreG1vJB%2Fimage.png?alt=media&#x26;token=6dd42a92-8f33-41fc-973f-0d3ee75892c7" alt=""><figcaption><p>Pagination by page</p></figcaption></figure>

### **Method 2: Recursive Requests (by field):**&#x20;

If the API requires data from the previous API call to get the following data, you will need to use Recursive to sync the data.

There are 3 recursive targets that you can use, Parameter, URL, and Body.

**Parameter:** If the API response contains something like next\_id: xxx, and you will need to use it as a parameter in the URL as the value of something like “startFrom”, you will need to set the parameter as “startFrom: {{next\_id}}”, then in the URL it will be ?startFrom={{next\_id}}

**URL:** If the API response contains the URL of the next page, by setting the JSON Path to the key for the URL, the API connector will call the next page automatically.

**Body:** So far, the only API we have used Body as Recursive Target is Notion. We will need to put “next\_cursor” from the previous API call as the “start\_cursor” of the next call to request body. Example: { “start\_cursor”: {{next\_cursor}} }

<figure><img src="https://lh6.googleusercontent.com/ka_meh5FcntaDRvz-AQueuI2T1UrzOg6f5HhNPztJpCEW7GaZW42Elml5lK7UoBRXPkjmQC5kYoBMt2ujw675vhUjF_jrwChU1x-edX3yCaEyU4k2LOGpsCspFxac_zoiRhn2vjpfWAHNm_RDgdlvoA" alt=""><figcaption></figcaption></figure>

### **Method 3: Loop**&#x20;

If your API requires something like an ID to retrieve details, you can loop through the IDs to get the details data. For example, assume you have a list of products, with ID, name, and price. You would like to get the details of the products, however, there is no such API like /product/details, but an API like /product/{{id}}/details.

You can set the Loop Target to Parameter, set the Loop Parameter Key to id, and select column “id” in the imported data of Products as Values to Loop. The Connector will loop through the “id”s in the table and replace the value within the bracket to request data.

* **Loop Target:** You can select to use it in the Parameter or Body. If you select Parameter, you will need to add `{{name}}` to your URL to use it. If you select Body and use the Post method, your Loop Parameter Key will be automatically added to your Post request body.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FpOPLaK4TBdAqBCnTMG9c%2Fimage.png?alt=media&#x26;token=a02533e8-4566-4351-9fc3-6154065f521f" alt=""><figcaption></figcaption></figure>

* **Loop Parameter Key:** The key name inside the curly brackets, `{{}}`, in your API URL.
* **Values to Loop:** The array of values to loop inside the brackets. The values should be referred from a column in your project table.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2Ft79jIc13jrfeQJ4nbiBJ%2Fimage.png?alt=media&#x26;token=c624e13b-9dbf-4fd5-85ea-1a303aa11106" alt=""><figcaption></figcaption></figure>

### 9. API Update

If your API data is real-time, you can turn on **API Update** to retrieve data at a certain time interval. There are three options in the settings of **API Update**.

* **Update Method:** refers to how to deal with the new data points and the current data points.

  * **Replace**: Every time Acho requests data through the API, it will replace the current dataset with the latest dataset entirely.
  * **Incremental**: Every time Acho requests data through the API, it will append the new data points to the current dataset. This method may generate duplicate data points.&#x20;

* **Sync Frequency**: refers to how often Acho should retrieve data through the API. There are three options that you can choose from:
  * **Intraday:** update the table every specific time interval within a day. The minimum time interval is 15 minutes.
  * **Daily:** update the table at a specific time (such as 6 a.m.) every day.&#x20;
  * **Custom:** you can define the scheduler in a more flexible way.

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-Mhn_eaghZohJ7dc9upD%2F-MhovfmR9I_Ftb_olGXY%2Fimage.png?alt=media\&token=fbe59b0f-3699-4379-995f-66399dce15ef)

## Set up dynamic date ranges

Acho supports setting up dynamic dates ranges without changing parameters or URLs manually. Currently, there are four dynamic date expressions on Acho:

* `{{year}}`: refers to the current year in UTC. The year is expressed in 4 digits, such as 2021.
* `{{month}}`: refers to the current month in UTC. The month is expressed in 2 digits, such as 08.
* `{{day}}`: refers to the current day in UTC. The month is expressed in 2 digits, such as 23.
* `{{today}}`: refers to today's date in the format of `yyyy-mm-dd` , such as 2021-08-22, in UTC.
* `{{today_full_date}}`: refers to today's date in the format of `yyyy_mm_dd`, such as 2021\_08\_22, in UTC.
* `{{yesterday}}`: refers to yesterday's date in the format of `yyyy-mm-dd`, such as 2021-08-22, in UTC.
* `{{yesterday_full_date}}`: refers to yesterday's date in the format of `yyyy_mm_dd`, such as 2021\_08\_22, in UTC.
* `{{tomorrow}}`: refers to tomorrow's date in the format of `yyyy-mm-dd`, such as 2021-08-22, in UTC.
* `{{tomorrow_full_date}}`: refers to tomorrow's date in the format of `yyyy_mm_dd`, such as 2021\_08\_22, in UTC.
* `{{unix_epoch_seconds}}` : refers to current unix epoch timestamp in seconds, such as 1712978322
* `{{unix_epoch_milliseconds}}` : refers to current unix epoch timestamp in milliseconds, such as 1712978322000

You can put the expression in URLs or Parameters. Here are the examples.

### URLs

* Change the month in the API URL to this month automatically.

```
http://api.example.com/xxx/{{year}}-{{month}}
```

* Change the date in the API URL to today's date.

```
http://api.example.com/xxx/{{today}}
```

* Change the date in the API URL to yesterday's date.

```
http://api.example.com/xxx/{{yesterday}}
```

###

### Parameters

If there are two parameter keys, startDate and endDate, to define the date range in your API, then you can use the dynamic date expression in both parameters. Here are some examples of how to use dynamic date expressions in Parameters.

* Retrieve data from 2021-01-01 to the current date

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FcF5CIeQIWPU1q4429vom%2Fimage.png?alt=media&#x26;token=7352113b-e4ef-498b-b06b-552f27829456" alt=""><figcaption></figcaption></figure>

* Retrieve data from 2021-01-01 to yesterday

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F8sQmCG7zTAXX4QGE4mGn%2Fimage.png?alt=media&#x26;token=cd8221b4-dcd6-48fa-b66d-a796b339bcb8" alt=""><figcaption></figcaption></figure>

* Only retrieve today's data

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F3IoDDzBJckDLuv67irG0%2Fimage.png?alt=media&#x26;token=6f3e0707-39cc-461d-830d-548e91fd6d73" alt=""><figcaption></figcaption></figure>

* Only retrieve yesterday's data

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F9s4YQ3tRbg14awmCRmVt%2Fimage.png?alt=media&#x26;token=9cca2701-5495-4487-956e-d0531fc19203" alt=""><figcaption></figcaption></figure>

* Only retrieve this month's data

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FsJuzQmJ5NXCwArS9WKew%2Fimage.png?alt=media&#x26;token=7f9b7010-1b92-4357-9b60-6ce645b204a7" alt=""><figcaption></figcaption></figure>

##
