> AI documentation index: [/llms.txt](/llms.txt)

# Fenris Authentication Guide

Fenris restricts access to its APIs and manages access tokens through an OAuth2 authorization server using the Client Credentials workflow. You will receive a Client ID and Client Secret used to obtain a bearer token from the Authorization Server.

Bearer tokens grant access to Fenris Services when included in the `Authorization` header of every request. Tokens are valid for **24 hours (86,400 seconds)** and must be refreshed after expiry.

## Token Request

**Endpoint:** `POST https://auth.fenrisd.com/realms/fenris/protocol/openid-connect/token`

**Required headers:**

| Header | Value |
|---|---|
| `Content-Type` | `application/x-www-form-urlencoded` |
| `Authorization` | `Basic <base64(client_id:client_secret)>` |

The `Authorization` value is the Base64 encoding of `client_id:client_secret` joined by a colon. Most HTTP libraries construct this automatically when you select Basic authentication and set the username to your Client ID and the password to your Client Secret.

**Request body:**

| Field | Value |
|---|---|
| `grant_type` | `client_credentials` |

**Response:**

```json
{
  "token_type": "Bearer",
  "expires_in": 86400,
  "access_token": "..."
}
```

Use the `access_token` value as the bearer token in all subsequent API requests:

```
Authorization: Bearer <access_token>
```

## Making Authenticated Requests

Every Fenris API request must include the bearer token in the `Authorization` header:

```
Authorization: Bearer <access_token>
```

Tokens expire after 24 hours. Implement token caching and refresh logic to avoid request failures at expiry. See [OAuth2 refresh strategies](https://fenrisd.com/blog/how-to-refresh-oauth2-tokens-the-smart-way-4-strategies-for-fenris-api-authentication/) for production patterns.

Contact [customersuccess@fenrisd.com](mailto:customersuccess@fenrisd.com) to obtain Client ID and Client Secret credentials.

## Code Samples

### Python

```python
import requests
import base64

client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"

basic_string = client_id + ":" + client_secret
basic_string_encoded = base64.b64encode(basic_string.encode()).decode()

headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": f"Basic {basic_string_encoded}"
}

data = {
    "grant_type": "client_credentials",
}

response = requests.post(
    "https://auth.fenrisd.com/realms/fenris/protocol/openid-connect/token",
    headers=headers,
    data=data
)

token = response.json()["access_token"]
print(token)
```

### JavaScript

```javascript
const axios = require("axios");
const base64 = require("base64-js");

const client_id = "YOUR_CLIENT_ID";
const client_secret = "YOUR_CLIENT_SECRET";

const basic_string = client_id + ":" + client_secret;
const basic_string_encoded = base64.fromByteArray(new TextEncoder().encode(basic_string));

const headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": `Basic ${basic_string_encoded}`
};

const data = {
    grant_type: "client_credentials",
};

axios
    .post(
        "https://auth.fenrisd.com/realms/fenris/protocol/openid-connect/token",
        data,
        { headers }
    )
    .then(response => {
        const token = response.data.access_token;
        console.log(token);
    })
    .catch(error => {
        console.log(error);
    });
```

### Java

```java
import java.io.IOException;
import java.util.Base64;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class TokenFetcher {

    public static void main(String[] args) throws IOException {
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";

        String basicString = clientId + ":" + clientSecret;
        String basicStringEncoded = Base64.getEncoder().encodeToString(basicString.getBytes());

        HttpPost post = new HttpPost(
            "https://auth.fenrisd.com/realms/fenris/protocol/openid-connect/token"
        );
        post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
        post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicStringEncoded);

        StringEntity requestEntity = new StringEntity("grant_type=client_credentials");
        post.setEntity(requestEntity);

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
             CloseableHttpResponse response = httpClient.execute(post)) {
            String responseString = EntityUtils.toString(response.getEntity());
            System.out.println(responseString);
        }
    }
}
```

### C#

```csharp
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

string clientId = "YOUR_CLIENT_ID";
string clientSecret = "YOUR_CLIENT_SECRET";

string basicString = clientId + ":" + clientSecret;
var basicStringEncoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(basicString));

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
    client.DefaultRequestHeaders.Add("Authorization", "Basic " + basicStringEncoded);

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
    });

    var response = await client.PostAsync(
        "https://auth.fenrisd.com/realms/fenris/protocol/openid-connect/token",
        content
    );

    var responseString = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseString);
}
```

### Ruby

```ruby
require 'net/http'
require 'json'
require 'base64'

client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"

basic_string = "#{client_id}:#{client_secret}"
basic_string_encoded = Base64.strict_encode64(basic_string)

uri = URI("https://auth.fenrisd.com/realms/fenris/protocol/openid-connect/token")

req = Net::HTTP::Post.new(uri)
req.content_type = "application/x-www-form-urlencoded"
req.add_field("Authorization", "Basic #{basic_string_encoded}")
req.set_form_data("grant_type" => "client_credentials")

res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http|
    http.request(req)
}

token = JSON.parse(res.body)['access_token']
puts token
```
