> For the complete documentation index, see [llms.txt](https://docs.tnid.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tnid.com/authentication/user-authentication.md).

# User Authentication

Users of TNID (as opposed to Companies) are currently able to generate an access token using an OTP to their telephone number.

{% hint style="warning" %}
We do not expire tokens or force password resets at any time. We follow the latest NIST guidelines and believe in a strong, lengthy secure password protocol. Create new Client Secrets if you believe there is evidence of a compromise.
{% endhint %}

### Generate Your Access Token

The below URLs are available to create your API token, depending on your environment:

{% embed url="<https://api.staging.v2.tnid.com/auth/create_user_otp>" %}
Staging Environment
{% endembed %}

{% embed url="<https://api.demo.v2.tnid.com/auth/create_user_otp>" %}
Demo Environment
{% endembed %}

Make a request to /auth/create\_user\_otp with the telephone\_number of an existing user or a not existing user and optional information:

```
{
   "telephone_number": "3024343434333",
   "first_name": "John",
   "last_name": "Doe",
   "email": "email@address.com"
}

```

Make a request with the telephone\_number and the received otp\_code to /auth/token:

```
{
   "telephone_number": "3024343434333",
   "otp_code": "698125"
}

```

Use the access\_token from the response to interact with the GraphQL api at /user.

## Example Code

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

```
import requests

# courtesy of https://github.com/PockyBum522/
# You will need to run this as step one, after which the user will receive an OTP in their email.
# With that OTP, you can request the actual token
def get_bearer_token_otp_for_existing_user(user_phone_number):
    url = "https://api.staging.v2.tnid.com/auth/create_user_otp"

    # Define the headers and body of the request
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }

    # The payload or data to be sent in the POST request
    data = {
        "telephone_number": user_phone_number
    }

    # Make the POST request to the token endpoint
    response = requests.post(url, headers=headers, data=data)

    # Check if the request was successful
    if response.status_code == 200:
        print(f"Response OK when requesting OTP: {response.status_code} {response.text}")
    else:
        raise Exception(f"Failed to request OTP: {response.status_code} {response.text}")


# This is step two, to request the bearer token once you have an OTP
def get_bearer_token(user_phone_number, otp):
    url = "https://api.staging.v2.tnid.com/auth/token"

    # Define the headers and body of the request
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }

    # The payload or data to be sent in the POST request
    data = {
        "telephone_number": user_phone_number,
        "otp_code": otp
    }

    # Make the POST request to the token endpoint
    response = requests.post(url, headers=headers, data=data)

    # Check if the request was successful
    if response.status_code == 200:
        # Parse the response and extract the access token
        token_data = response.json()
        return token_data.get("access_token")
    else:
        raise Exception(f"Failed to retrieve token: {response.status_code} {response.text}")


# Create a new user, after which you will have to request their OTP, then bearer token using the other provided functions
def create_new_user(phone_number, first_name, last_name, email):
    url = "https://api.staging.v2.tnid.com/auth/create_user_otp"

    # Define the headers of the request
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }

    # The payload or data to be sent in the POST request
    data = {
        "telephone_number": phone_number,
        "first_name": first_name,
        "last_name": last_name,
        "email": email
    }

    # Make the POST request to the token endpoint
    response = requests.post(url, headers=headers, data=data)

    # Check if the request was successful
    if response.status_code == 200:
        print(f"Response OK when creating new user: {response.status_code} {response.text}")
    else:
        raise Exception(f"Failed to create new user: {response.status_code} {response.text}")


query_phone_number = "14075554530"

get_bearer_token_otp_for_existing_user(query_phone_number)

print("Please check your email and enter the OTP here: ")
otp_from_email = input()

bearer_token = get_bearer_token(query_phone_number, otp_from_email)
print("Bearer Token: ", bearer_token)

# OR:
# new_user_phone_number = "15555555555"
#
# create_new_user(new_user_phone_number, "Firstname", "Lastname", "user@email.com")
#
# get_bearer_token_otp_for_existing_user(new_user_phone_number)
#
# print("Please check your email and enter the OTP here: ")
# otp_from_email = input()
#
# bearer_token = get_bearer_token(new_user_phone_number, otp_from_email)
# print("Bearer Token: ", bearer_token)
```

{% endtab %}
{% endtabs %}
