# Using basic authentication

Before calling other endpoints, you need to generate an Authorization Bearer token. To retrieve your token using basic auth, simply send your account credentials in base64 encoded format within the header.

The format should be ***Basic email:password*** where email:password are encoded.

This token has a lifetime of 1 year, and is invalidated whenever a new token is generated.

The token is attached to the user credentials it was created with and as such it is recommended to create a service account for applications and use those credentials for token generation.&#x20;

***

## Generate a bearer token using basic authentication

<mark style="color:green;">`POST`</mark> `/api-basic-auth`

Generate a token for use with other endpoints.

#### Header

| Name          | Value                   |
| ------------- | ----------------------- |
| Content-Type  | `multipart/form-data`   |
| Authorization | `Basic (base64 string)` |

#### Request

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

```sh
curl --location 'https://enterprise.personr.co/api/1.1/wf/api-basic-auth' \
-H "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ="
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
const axios = require('axios');
const base64 = require('base-64');

const email = 'example@personr.co';
const password = 'example123';

const credentials = base64.encode(`${email}:${password}`);

const config = {
    headers: {
        'Authorization': `Basic ${credentials}`
    }
};

axios.get('https://enterprise.personr.co/api/1.1/wf/api-basic-auth', config)
    .then(response => {
        console.log('Response:', response.data);
    })
    .catch(error => {
        console.error('Error:', error);
    });
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
const email = 'example@personr.co';
const password = 'example123';

const credentials = btoa(`${email}:${password}`);

const headers = new Headers();
headers.append('Authorization', `Basic ${credentials}`);
headers.append('Content-Type', 'application/json');

fetch('https://enterprise.personr.co/api/1.1/wf/api-basic-auth', {
    method: 'POST',
    headers: headers
})
.then(response => response.json())
.then(data => {
    console.log('Response:', data);
})
.catch(error => {
    console.error('Error:', error);
});
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require "uri"
require "net/http"
require "base64"

email = "example@personr.co"
password = "example123"

credentials = Base64.strict_encode64("#{email}:#{password}")

url = URI("https://enterprise.personr.co/api/1.1/wf/api-basic-auth")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = "Basic #{credentials}"

response = https.request(request)
puts response.read_body
```

{% endtab %}

{% tab title="Python" %}

```python
import requests
import base64

email = 'example@personr.co'
password = 'example123'

credentials = base64.b64encode(f'{email}:{password}'.encode()).decode()

url = "https://enterprise.personr.co/api/1.1/wf/api-basic-auth"

headers = {
    'Authorization': f'Basic {credentials}'
}

response = requests.post(url, headers=headers)

print(response.text)
```

{% endtab %}
{% endtabs %}

**Response**

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

```json
{
  "status": "success",
  "response": {
    "token": "bus|4242424242424x424242424242424242|4343434343434x434343434343434343",
    "user_id": "4242424242424x424242424242424242",
    "expires": 31536000
  }
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "statusCode": 400,
    "reason": "INVALID_LOGIN_CREDENTIALS",
    "message": "We didn’t find an account with those login credentials",
    }
```

{% endtab %}
{% endtabs %}

In the response, you'll receive:

* The bearer token
* The unique user id
* The expiration time of your token, expressed in seconds

{% hint style="info" %}
Remember to include your token in the header of every subsequent API call, in the format **Authorization: Bearer YOUR\_TOKEN**
{% endhint %}

{% hint style="info" %}
Don't forget to set up a workflow to renew your token before it expires.
{% endhint %}
