> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add JWT machine-to-machine authentication

> How to configure JWT machine-to-machine authentication in Atoti Limits using the built-in support or a custom ILimitsRestClientBuilder, covering the service-principal property and custom request headers

## Overview

Atoti Limits includes native support for [JSON Web Tokens](https://auth0.com/docs/secure/tokens/json-web-tokens).
We can utilize this support to send MtM requests using the `Authorization: Jwt ...` header.

> [!NOTE]
>
> This is the default authentication mechanism in Atoti Limits. To authenticate machine-to-machine requests differently,
> see [Use a custom authentication](#use-a-custom-authentication).

## Use the default support

To use the native support, specify the following properties in **both Atoti Limits and your connected server**:

```yml theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
limits:
  autoconfiguration:
    service-principal: admin // this specifies the principal user designated to invoke MtM request with JWT MtM authentication
```

## Use a custom authentication

As described in [Adding Custom Machine-to-Machine (MtM) Authentication](./../custom-mtm-authentication#how-to-add-custom-mtm-authentication),
you can replace the default mechanism with your own.

### 1. Add a custom `ILimitsRestClientBuilder` bean in the Connected Server and Atoti Limits

`ILimitsRestClientBuilder` has a single method, `getRestClientBuilder()`, which returns the `RestClient.Builder` used for
machine-to-machine requests. Set the headers your authentication needs on that builder. For example, to send an API key
read from a property:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
public class ApiKeyRestClientBuilder implements ILimitsRestClientBuilder {

  private final RestClient.Builder restClientBuilder;

  public ApiKeyRestClientBuilder(@Value("${my-company.mtm.api-key}") String apiKey) {
    this.restClientBuilder = RestClient.builder().defaultHeader("X-API-Key", apiKey);
  }

  @Override
  public RestClient.Builder getRestClientBuilder() {
    return restClientBuilder;
  }
}
```

The receiving server must accept this header, so configure its security accordingly.

### 2. Expose your custom `ILimitsRestClientBuilder` bean in the Spring context

Once you have written your bean you can [expose it](../../extend#import-spring-beans-into-the-project)
to the Spring context.
