# Set up an API POST request - Xano

{% hint style="info" %}
[Upgrade your free account](https://docs.bravostudio.app/get-started/unlock-power-features-to-create-interactive-apps) to the **Bravo Solo plan** to use this feature.
{% endhint %}

The **Data Collections** feature enables you to create API requests to any database or tool with an available REST API. In this guide, we'll show you how to set up a **POST request** to create new data in an external database from your app.

{% hint style="info" %}
You can use other API verbs (i.e. **PUT** or **POST**) to create data if your API specifies otherwise. Simply change the verb type of the request URL.
{% endhint %}

#### :point\_right: **Example** **Figma file**

If you don't have a design yet, import it to Bravo. You can also duplicate this [Figma file](https://www.figma.com/design/mNyTDu2CK9wsVLRGUCmecH/Edit-your-profile?node-id=0-1\&t=LOqUc58FSodnzyIz-0).

#### :point\_right: **Database**

In this guide, we're using Xano database to manage our app's backend. The Xano platform offers a powerful and flexible solution to handle data efficiently.\
\
Our focus will be in the user table, which is a critical component in most apps as it stores user related information.&#x20;

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F09EVKCut1JXqpkmwanIx%2FScreenshot%202024-06-27%20at%2011.12.17.png?alt=media&#x26;token=a34be363-8dbd-45cb-b907-6e7a048b241e" alt=""><figcaption><p>User Database in Xano</p></figcaption></figure>

To facilitate various operations on this table, Xano has auto-generated CRUD (Create, Read, Update, Delete) operations. These operations are essential for managing the data lifecycle in our application.

The CRUD operations provided by Xano enable us to:

* **Create**: Add new user entries to the database.
* **Read**: Retrieve user information stored in the database.
* **Update**: Modify existing user data.
* **Delete**: Remove user entries from the database.

If you are using a different backend tool, refer to its API documentation to understand how to construct the request, including both the URL and the body.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F76lN5JGYiMy5trdcgNrf%2FScreenshot%202024-06-27%20at%2011.16.11.png?alt=media&#x26;token=758ef466-1bbb-4a0b-a722-d8dcffbcdaa6" alt=""><figcaption><p>User CRUD endpoints autogenerated in Xano</p></figcaption></figure>

To set up the POST request in the backend, follow [Xano's API documentation.](https://docs.xano.com/api/the-basics/crud-operations) This example also demonstrates how to use the required **JSON body** to edit a record in the table.

## How to set up a PATCH request

1\. In **Data Library**, click **Create a New Collection** and choose **Start from scratch**. If you already have a collection, select the desired API collection.

{% hint style="success" %}
You can also import your API collection using the Xano Wizard, Open API, Postman or Swagger.
{% endhint %}

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FPCuc05VVPTG88KYsfDss%2FScreenshot%202024-06-27%20at%2011.50.47.png?alt=media&#x26;token=736cfa95-6f50-4155-b200-36e4a2bf5123" alt=""><figcaption><p>Create a new collection</p></figcaption></figure>

2. Provide a name and description for the new collection. If this step is already completed, you can skip it.
3. The API documentation specifies any **Headers**, **Parameters**, and/or **Body** value needed in order to make the POST request. In this example, Xano uses the [bearer token method](https://www.oauth.com/oauth2-servers/differences-between-oauth-1-2/bearer-tokens/) to authenticate the request. In **Collection settings**, set the **Authentication type** to **Bearer** and leave the **Token** field empty. If this step is already completed, you can skip it.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F92Fho6UpUhz4tsnbtGsn%2FScreenshot%202024-06-26%20at%2016.15.46.png?alt=media&#x26;token=a984b1c6-4912-4195-ae51-2585680a3067" alt=""><figcaption><p>Collection settings - Bearer Token</p></figcaption></figure>

4. To create a new request, click the **+** button in your collection and enter a name, such as "Add new item".
5. To add a new item to the table in Xano, use a POST API request and copy the autogenerated endpoint URL.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FNIxoLlkRdoWnX71UcCWe%2FScreenshot%202024-07-18%20at%2011.42.10.png?alt=media&#x26;token=94c2c540-e384-4b88-b493-b490081283e3" alt=""><figcaption></figcaption></figure>

The typical Xano endpoint structure is:

```plaintext
https://api.xano.com/your-endpoint/scores
```

6. Return to Bravo, choose `POST` as the request type, and paste the endpoint URL.
7. As this is a POST request, we need a JSON body. Go back to Xano, click on the **Run & Debug** button, and copy the existing JSON body.
8. Paste the JSON copied from Xano into the JSON section of the API request body in Bravo.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FI5Mjkbe3uQ37hUXkPC4l%2FScreenshot%202024-07-18%20at%2011.46.33.png?alt=media&#x26;token=4b4c1fbe-0b31-42a4-8533-477be08e8344" alt=""><figcaption><p>POST request - JSON body</p></figcaption></figure>

9. We will replace the values in the example request body with variables while keeping the same key names ("name", "score"), as these match the Xano column names.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2Fr82qPL7MCg1c1WLB7c3Z%2FScreenshot%202024-07-18%20at%2011.49.10.png?alt=media&#x26;token=5f80d01d-5c61-4dec-901b-b46db09e4f9d" alt=""><figcaption></figcaption></figure>

To define a [**variable**](https://docs.bravostudio.app/connect-api/request-url-variables), we'll use this notation: <mark style="color:red;">`${input}`</mark>. In our case, we'll define the variables <mark style="color:red;">`${name}`</mark> ,<mark style="color:red;">`${score}`</mark> but you can use any variable name you prefer.

{% hint style="info" %}
**Permitted characters:** lowercase a-z, uppercase A-Z, 0 to 9, underscore (\_) and dash (-). **No spacing**.
{% endhint %}

Later on, when binding the UI elements of our app to the API requests, we'll specify which elements are bound to which variables. For instance, if we have a [form](https://www.figma.com/community/file/981976861747536392) connected to this POST request, we'll bind each input field to one of the variables we defined in the JSON body.

```
{
  "name": "${name}",
  "score": ${score}
}
```

{% hint style="warning" %}
For **text** input, include the quotation marks, like `"`<mark style="color:red;">`${input}`</mark>`".` For **number** input, do not include the quotations, like <mark style="color:red;">`${input}`</mark>
{% endhint %}

{% hint style="info" %}
You can use a [JSON validator](http://json-validator.com/) to make sure the JSON snippet you copied is syntactically correct, and avoid getting errors later on.
{% endhint %}

10. &#x20;Send a test request to the database to ensure it is correctly constructed. Navigate to the **Input Variables & Test Values** tab within the API request and provide sample data for the previously defined variables in the JSON body. In this example: `Mary` for `${name}` and `42` for `${score}`.

{% hint style="info" %}
The key name for the **Test Value** must match the defined Variable Name exactly, including case sensitivity.
{% endhint %}

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FHBcjakI8jwa1H4d3A3lV%2FScreenshot%202024-07-18%20at%2011.49.57.png?alt=media&#x26;token=56758004-fc73-4d84-8053-f0209a2fa831" alt=""><figcaption><p>Input Variables &#x26; Test Values</p></figcaption></figure>

If the POST request is configured correctly, the example content will be stored in your database.\
\
11\. In step 2, we set the **Authentication type** to **Bearer** and left the **Token field** **empty.** To authenticate the request, go to the "**Input Variables & Test Values**" tab, add "`_authorization`" as variable and enter the `token` in the value field.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2Frv7cidQdahaxPDliYoWg%2FScreenshot%202024-07-18%20at%2012.01.32.png?alt=media&#x26;token=9a9b0bc9-9919-41c4-82db-30273c8bd751" alt=""><figcaption><p>Authenticating API request</p></figcaption></figure>

12. Once the values are set and the request is authenticated, click **Send**.&#x20;

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FdEq38iTCXhVEsctGNh2R%2FScreenshot%202024-07-18%20at%2012.04.40.png?alt=media&#x26;token=bb732fec-773e-49ea-a232-723850599c54" alt=""><figcaption><p>scores database after sending a post request</p></figcaption></figure>

13. If the request is successful, you'll receive a Response message containing data from the API endpoint. This data will depend on the backend configuration. In this example, you'll get data corresponding to the new record created.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F8kQMNU65In23XRelsKuy%2FScreenshot%202024-07-18%20at%2011.50.57.png?alt=media&#x26;token=3d2c4441-6420-4484-81af-913ea23df639" alt=""><figcaption><p>Selected data in the Received data tab of an API request</p></figcaption></figure>

Also a new record will be created with the provided test data in your database.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2F4jPOPWcQlV24O6fkeza6%2FScreenshot%202024-07-18%20at%2012.04.40.png?alt=media&#x26;token=92aced5f-b488-42c7-90a4-df9851235f26" alt=""><figcaption><p>scores table in xano</p></figcaption></figure>

14. To verify the HTTP response body, go to the **Debug & Inspect Response** tab. A <mark style="color:green;">`Status Code: 200 - OK`</mark> indicates successful data retrieval.

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FFDSPjljHDgSNcS2xhTHZ%2FScreenshot%202024-07-18%20at%2011.51.20.png?alt=media&#x26;token=82dff206-d506-4a2c-b463-9c77448e6695" alt=""><figcaption></figcaption></figure>

On the other hand, if the request was not successful, you will get an [HTTP error](https://docs.bravostudio.app/good-to-know/troubleshoot/errors-in-api-requests) as a response. In this case, r**eview and double check your JSON body and URL to be correct**.

15. Finally, rename the variables in the **Output Variables** section to align with the user table API request names for proper data binding. More about [Variables](https://docs.bravostudio.app/connect-api/request-url-variables).

<figure><img src="https://1444025092-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MXpqEanvOhm8vMtIH8i%2Fuploads%2FhKQgSfEWbrylViwV5uAw%2FScreenshot%202024-07-18%20at%2011.51.07.png?alt=media&#x26;token=d9b1d29c-0ddb-4872-b137-c83570de957f" alt=""><figcaption><p>Output Variables of a PATCH API request</p></figcaption></figure>

After setting up the API request, bind the form using the guide below:

{% embed url="<https://docs.bravostudio.app/bravo-tags/form-and-input-fields/data-biding-send-form-input-to-a-database>" %}
