# API Authentication

The first step to integrate with Unitary's API is setting up authentication. Unitary's API provides an authentication endpoint which uses your API key to generate a Bearer token that must be included in any subsequent API requests you wish to make.

The endpoint reference below gives more detail about how to use the authentication endpoint, how to use the returned Bearer token, and the expiration of tokens.

{% hint style="info" %}
This API Authentication guide assumes you have your API key already set up. If this is not the case, please email at <support@unitary.ai>.
{% endhint %}

### Endpoint reference

{% openapi src="/files/Qi5eEXKOFsvbyKUL4QLW" path="/authenticate" method="post" %}
[api-worker.autogenerated.openapi.yaml](https://1303016406-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgkYGyPKqtHGZyEvMl1GP%2Fuploads%2Fgit-blob-1ea297cbed161d2a16d21ca42ffa258cb07caefb%2Fapi-worker.autogenerated.openapi.yaml?alt=media)
{% endopenapi %}

### Example code

{% tabs %}
{% tab title="cURL" %}

<pre class="language-bash"><code class="lang-bash"><strong>curl --location --request POST 'https://api.unitary.ai/v1/authenticate'
</strong>--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode 'key={API_KEY}' 
</code></pre>

{% endtab %}

{% tab title="Python" %}

```python
import requests
url = "https://api.unitary.ai/v1/authenticate"
API_KEY = "<api_key>"

payload = f"key={API_KEY}"
headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
response = requests.request("POST", url, headers=headers, data=payload)

token = response.text["api_token"]
expiry = response.text["expiry"]

print(token, expiry)
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
const fetch = require('node-fetch'); // Make sure you have 'node-fetch' installed for this to work
const url = "https://api.unitary.ai/v1/authenticate";
const API_KEY = "<api_key>";
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
const body = `key=${API_KEY}`;

fetch(url, { method: 'POST', headers: headers, body: body })
  .then(response => response.json())
  .then(json => {
    const token = json.api_token;
    const expiry = json.expiry;
    console.log(token, expiry);
  })
  .catch(error => console.error('error:', error));
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.unitary.ai/api-references/authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
