> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cloud.cdata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Example API

## Summary

This page acts as an example of the steps required to configure a connection to an API from start to finish. This example uses an API that contains people’s maintenance schedules for their vehicles.

## Create the API Connection

First, create a new API connection.

<Steps>
  <Step>
    Open the **Sources** page of the Connect AI navigation menu.
  </Step>

  <Step>
    Click **+ Add Connection** in the upper-right corner.
  </Step>

  <Step>
    In the **Add Connection** dialog that opens, type *API* to display the API connector tile, then click the tile.
  </Step>

  <Step>
    On the **Global Settings Tab**, name the connection. This example uses "TestAPI."
  </Step>

  <Step>
    Select the Authentication method. Since no authentication is required for this API, select `No Auth`.
  </Step>

  <Step>
    Leave this section blank, because no headers are required for this connection.
  </Step>

  <Step>
    Set the pagination. This API uses a next page URL, providing the next page URL as part of the response. The following example shows the relevant part of the response from the Vehicles endpoint:

    ```json theme={null}
    {
      "people": [
        {
            ...
        },
        ...
      ],
      "nextLink": "https://my_vehicles/api?$skiptoken='ERNSH'"
    }
    ```
  </Step>

  <Step>
    Set the Pagination **Type** to `URL`.
  </Step>

  <Step>
    Set the URL Path to `/nextLink`.
  </Step>

  <Step>
    Click the **Create** button at the top of the page to save the API connection.
  </Step>
</Steps>

## Create the Table

Now that you've added a connection corresponding to a catalog, you can create tables for each of the API's endpoints.

<Steps>
  <Step>
    In your API connection, click **Tables** to open the Tables tab.
  </Step>

  <Step>
    Click **Add** to add a new table.
  </Step>

  <Step>
    Provide a **Name** for your table. This is the name that the table displays when accessing it from clients. For example: `Vehicles`.
  </Step>

  <Step>
    Leave the **Response Type** as `JSON` and the **Request** call as `GET`.
  </Step>

  <Step>
    Set the **Request URL** to your API endpoint URL. This example uses `https://my_vehicles/api`.
  </Step>

  <Step>
    Leave the **Parameters** section blank, since there are none for this API.
  </Step>

  <Step>
    Similarly, leave **Headers** blank.
  </Step>

  <Step>
    In the **Table Data** section, click **Configure** to automatically fill out the **Table Data** information. This opens the **Preview Request**, which shows the following API response:

    ```json expandable theme={null}
    {
      "people": [
        {
          "personal": {
            "age": 21,
            "gender": "M",
            "name": {
              "first": "John",
              "last": "Doe"
            }
          },
          "vehicles": [
            {
              "type": "car",
              "model": "Honda Civic",
              "insurance": {
                "company": "ABC Insurance",
                "policy_num": "12345"
              },
              "features": [
                "sunroof",
                "rims"
              ],
              "maintenance": [
                {
                  "date": "07-17-2017",
                  "desc": "oil change"
                },
                {
                  "date": "01-03-2018",
                  "desc": "new tires"
                }
              ]
            },
            {
              "type": "truck",
              "model": "Dodge Ram",
              "insurance": {
                "company": "ABC Insurance",
                "policy_num": "12345"
              },
              "features": [
                "lift kit",
                "tow package"
              ],
              "maintenance": [
                {
                  "date": "08-27-2017",
                  "desc": "new tires"
                },
                {
                  "date": "01-08-2018",
                  "desc": "oil change"
                }
              ]
            }
          ]
        },
        ...
      ]
    }
    ```
  </Step>

  <Step>
    Click **Next** to access the Add Columns menu. Here, you need to select a root path, and any columns to expose.
    The **Root Path** is the JSON element that contains repeating sub-elements and determines the rows of the table. In this case, you need to select multiple root paths because multiple elements repeat in this document. Because the root elements are nested, some data repeats for each row, as shown in this example. Select the `people` element first, because it repeats for each person, as shown in the following snippet from the response:

    ```json theme={null}
    {
         "people": [
             {
             <Person 1 data>
             },
             {
             <Person 2 data>
             }, 
             ...
         ]
     }
    ```

    Also select the `vehicles` and `maintenance` elements, because data from those two repeated sections is also needed.
    *Note: The JSON array uses square brackets `[]` to contain an array of JSON objects which are each contained within angle brackets `{}`.*
  </Step>

  <Step>
    For this example, select `first`, `last` from the people array, `type`, `model` from the vehicles array, and `date`, `description` from the maintenance array as the columns to expose.
  </Step>

  <Step>
    Click **Next** to access the **Table Preview**. This renders the first 10 rows of the table as currently configured. If configured correctly, you should see data for each column selected:

    | first | last | type  | model       | date       | description |
    | :---- | :--- | :---- | :---------- | :--------- | :---------- |
    | John  | Doe  | car   | Honda Civic | 07-17-2017 | oil change  |
    | John  | Doe  | car   | Honda Civic | 01-03-2018 | new tires   |
    | John  | Doe  | truck | Dodge Ram   | 08-27-2017 | new tires   |
    | John  | Doe  | truck | Dodge Ram   | 01-08-2018 | oil change  |
  </Step>

  <Step>
    Click **Confirm** to automatically add your **Table Data** configuration. The data selected should be reflected in the **Root Path** and **Columns** fields in the **Table Data** section.
  </Step>

  <Step>
    Click **Save** to save this table.
  </Step>
</Steps>

## Test Queries

After configuring your connection and your table, you can test it by running queries in the **Data Explorer**.

<Steps>
  <Step>
    In Connect AI, navigate to **Data Explorer**.
  </Step>

  <Step>
    In the **Connections** list, select the `TestAPI` connection created earlier. This expands all of that connection's tables, including the `Vehicles` table created in this example.
  </Step>

  <Step>
    Click the ellipsis next to this table and select **query**. This automatically generates the SQL query to retrieve all vehicles:

    * `SELECT * FROM [TestAPI].[REST].[Vehicles]` You can modify this SQL query as needed. For example, you can add a `WHERE` clause to select only vehicles of a specific model:
    * `SELECT * FROM [TestAPI].[REST].[Vehicles] WHERE model='Honda Civic'`
  </Step>

  <Step>
    Click **Execute** to run this query. If everything is configured correctly, the results are visible immediately.
  </Step>
</Steps>

To connect to this API data from your preferred client, refer to the details documented for the specific client on the **Integrations** page. To access this data, you might need to set the catalog (or database, depending on the client) to `TestAPI`.
