---
title: "Authentication"
description: "In this section, you will find comprehensive information about the process of authentication to the NaaS API."
order: 2
---

# Authentication

In this section, you will find comprehensive information about the process of CoinAPI authentication. 
It covers the fundamental aspects and procedures involved in obtaining authentication for CoinAPI usage. 
Whether you are new to CoinAPI or seeking to enhance your understanding of the authentication process, this section will provide you with a valuable overview of the topic.

There are different types of authentication methods available to secure and control access to its services.

- `API Key` - fundamental method of authentication that involves the usage of an API key for authentication and access control.

The console shows a key as `abk_`, 32 hex characters and an 8 hex character checksum, for example `abk_3f2504e04f8941d39a0c0305e82c3301582005f7`. Letter case does not matter. Send the value from the console. Keys that were already in use also still authenticate when sent as a GUID. A value that starts with `abk_` but has a wrong checksum is rejected.

- `JWT token` - extended authentication method for a JWT API key. You sign short-lived JWT tokens with your own private key or secret, and each token names its API key with the key ID in its `kid` header.
A request then carries only the `Authorization: Bearer JWT_TOKEN` header, so the API key itself never has to leave your servers.
Useful when you need to hand credentials to clients you do not control, for example when accessing the CoinAPI endpoints from the frontend JavaScript code exposed to the end-users.

## Authentication methods supported by the API

Here's an overview of the various products offered by CoinAPI and the supported authentication methods for each product.
To use resources that require authorized access, you will need to use one of the many authentication methods described below.


| Product name | API |      X-CoinAPI-Key header | Query param | URL path | Authorization header | Basic auth | JWT 
| --- | --- |               --- | --- | --- | --- | --- | --- | 
| `Node` | `JSON-RPC` |  ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 
| `Node` | `WebSocket` |  ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 

> [!NOTE]
> A JWT token is sent alone, in the `Authorization: Bearer JWT_TOKEN` header. Its `kid` header names your API key, so the request does not need the API key. Integrations that send the API key in a custom header, the query string or the URL together with the token keep working. The API key cannot be sent in the `Authorization` header together with a token.

### X-CoinAPI-Key header

You can authorize by providing an additional custom header named `X-CoinAPI-Key` and API key as its value.

Assuming that your API key is `abk_3f2504e04f8941d39a0c0305e82c3301582005f7`, then the authentication header you should send to us will look like:

`X-CoinAPI-Key: abk_3f2504e04f8941d39a0c0305e82c3301582005f7`

### Query string parameter (apikey)

You can authorize by providing an additional parameter named `apikey` with a value equal to your API key in the query string of your HTTP request.

Assuming that your API key is `abk_3f2504e04f8941d39a0c0305e82c3301582005f7` and that you want to request all exchange rates from `BTC` asset, then your query string should look like this: `GET /v1/exchangerate/BTC?apikey=abk_3f2504e04f8941d39a0c0305e82c3301582005f7`

> [!WARNING]
> The Query string method may be more practical for development activities.### URL path 

To ensure proper authentication when passing the API key in the URL, it is important to format it correctly. 
The real API key should always be preceded by the `APIKEY-` prefix, followed by the actual API key.
For example, if your API key is `abk_3f2504e04f8941d39a0c0305e82c3301582005f7`, the URL should be structured as `/APIKEY-abk_3f2504e04f8941d39a0c0305e82c3301582005f7`.

The API key can be placed at the beginning, middle, or end of the URL path. Note that the key is completely ignored when determining the path of the requested resource. Here are examples of the different placements:

- At the beginning of the URL:
  - **/**`APIKEY-abk_3f2504e04f8941d39a0c0305e82c3301582005f7`**/v1/exchanges**
- In the middle of the URL:
  - **/v1/**`APIKEY-abk_3f2504e04f8941d39a0c0305e82c3301582005f7`**/exchanges**
- At the end of the URL:
  - **/v1/exchanges/**`APIKEY-abk_3f2504e04f8941d39a0c0305e82c3301582005f7`
  
Please make sure to replace `abk_3f2504e04f8941d39a0c0305e82c3301582005f7` with your actual API key for authentication and access to the requested resources and remember to include the `APIKEY-` prefix before your real API key.

### Authorization header

This method involves including the API key directly in the Authorization header of the API request.

`Authorization: abk_3f2504e04f8941d39a0c0305e82c3301582005f7`

### Basic auth

Use Basic Authentication by setting the Authorization header with a base64 encoded string that combines your API key with a username (coinapi):

`Authorization: Basic Y29pbmFwaTphYmtfM2YyNTA0ZTA0Zjg5NDFkMzlhMGMwMzA1ZTgyYzMzMDE1ODIwMDVmNw==`

In this example, `Y29pbmFwaTphYmtfM2YyNTA0ZTA0Zjg5NDFkMzlhMGMwMzA1ZTgyYzMzMDE1ODIwMDVmNw==` is a base64 encoded string of the username and API key (coinapi:abk_3f2504e04f8941d39a0c0305e82c3301582005f7).

### JWT token
 
The JWT authentication method provides a secure and efficient way to authorize requests using JSON Web Tokens (JWT). 
With this method, JWTs can be passed via the Authorization header in the `Bearer JWT_TOKEN` format.
The token is the only credential the request needs: its `kid` header holds the key ID of your JWT API key.

#### Supported JWT Algorithms

The JWT authentication method supports the following algorithms for JWT token verification with various key sizes:

- `RSASSA-PSS (e.g. PS256)`
- `RSASSA-PKCS1-v1_5 (e.g. RS256)`
- `ECDSA (e.g. ES256)`
- `HMAC (e.g. HS256)`

#### Enabling JWT authentication

To enable JWT authentication for your API key:

1. In the customer portal, create a JWT key and import your public key (RSA or ECDSA), or the shared secret for HMAC.
2. Copy the key's Key ID from the API keys page of the customer portal.
3. Generate a JWT token with the Key ID in its `kid` header, signed with your private key or secret (it ensures the authenticity and integrity of the token).
4. Send the token in the `Authorization` header of your API requests using the `Bearer JWT_TOKEN` format. Do not send the API key with it.
5. Let the CoinAPI server find your key by the `kid` header and verify the token's signature with the key you imported in Step 1.

#### Example request with JWT authentication

To include a JWT in an API request, add the token to the `Authorization` header using the Bearer scheme. The format of the header is as follows:

     `Authorization: Bearer JWT_TOKEN`

Example request header with JWT token generated using RS256 algorithm:

```html
Authorization: Bearer eyJraWQiOiI5ODViMmQ0ZC1kMjE1LTQwN2MtODcxNi01NTIzNjA0YmM0ZTIiLCJhbGciOiJSUzI1NiJ9.ew0KICAic3ViIjogIjEyMzQ1Njc4OTAiLA0KICAibmFtZSI6ICJFWEFNUExFIFRPS0VOIiwNCiAgImlhdCI6IDE1MTYyMzkwMjIsDQogICJuYmYiOiAxNjg2MTM3MDI0LA0KICAiZXhwIjogMTcxNzY3MzAyNA0KfQ.CX6MWRSXQPKuQ_jrFCME91IwZhK8lq_2XrbDOyZ4-tPo0Ro52HA289sIfLo2LNafWQlq2lClfCN55TxyfC8n0xiifUwdec7g3kcGjCri6vTxaa8p6S3Fyyt2DxXccpi3Se4d_3mEQBZwMchKbQsw-W7Wj7njUk31ycgPQovvF4WrTuEYmhYw1sO9jCTORHmsSO7Shml7kv7AxlIUmzB2oq2KSmBhJV38Nz9oYj3KlPoMjgaIl4xYldNqnGyshh6fQyUQ1gQMQV6e4M5ro8YthjPOCvAT8yk77dTyOoE6Im58cAp6KtM-Gko-tWppUQTu-0M82LOvD_duP77n-hcoTw
```

Decoded JWT token header:

```json
{ "kid": "985b2d4d-d215-407c-8716-5523604bc4e2", "alg": "RS256" }
```

`kid` is the Key ID of your JWT API key. A request that sends only the token is rejected when `kid` is missing or names a different key.

Decoded JWT token payload:
```json
{
  "sub": "1234567890",
  "name": "EXAMPLE TOKEN",
  "iat": 1516239022,
  "nbf": 1686137024,
  "exp": 1717673024
}
```

#### Generating a token

The example below signs a token that is valid for 5 minutes with Python and [PyJWT](https://pyjwt.readthedocs.io/) (`pip install "pyjwt[crypto]"`).
Any JWT library works the same way: set `kid` in the token header, `nbf` and `exp` in the payload, and sign with your private key.

```python
import time
import jwt

KEY_ID = "985b2d4d-d215-407c-8716-5523604bc4e2"  # Key ID from the API keys page

with open("private_key.pem") as f:
    private_key = f.read()

now = int(time.time())
token = jwt.encode(
    {"nbf": now, "exp": now + 300},
    private_key,
    algorithm="RS256",
    headers={"kid": KEY_ID},
)

headers = {"Authorization": f"Bearer {token}"}
```

#### JWT Token expiration

To ensure the validity of JWT tokens and prevent the use of expired tokens, the inclusion of the following claims is required:

- `NBF (Not Before)`
  - The "nbf" claim specifies the earliest time at which the token can be considered valid. Tokens with a "nbf" claim set in the future should not be accepted.

- `EXP (Expiration Time)`
  - The "exp" claim defines the expiration time of the token. Tokens with an "exp" claim set in the past should not be accepted.

#### Request authentication 

During the process of request authentication, the CoinAPI server follows a series of steps to ensure secure access to its resources. 
Here's an overview of the authentication process:

1. Upon receiving the request, the CoinAPI server extracts the JWT from the `Authorization` header and finds your API key by the Key ID in the token's `kid` header.
2. Then, it verifies the JWT signature and checks the "NBF" and "EXP" claims to ensure token validity and expiration.
3. If the JWT is valid and not expired, the server authorizes the request and proceeds with the necessary processing to access CoinAPI resources.
4. If the JWT is invalid or expired, the server returns an appropriate error response.
