> For the complete documentation index, see [llms.txt](https://docs.brasilcashbank.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.brasilcashbank.com/pix-in/auth-token.md). Agents can also search and read this documentation through its [MCP server](https://docs.brasilcashbank.com/~gitbook/mcp).

# Auth Token

### Integration and Implementation Guide

#### 1. Overview

This document describes how to authenticate an application with the BrasilCashBank API using the OAuth Token endpoint.

The authentication process requires the application to provide its `client_id`, a Base64-encoded client certificate credential (`cert_client`), and the grant type.

Upon successful authentication, the API returns an access token that can be used to authorize subsequent API requests.

#### 2. API Endpoint

<table><thead><tr><th width="218">Property</th><th>Value</th></tr></thead><tbody><tr><td>HTTP Method</td><td>POST</td></tr><tr><td>Endpoint</td><td><code>https://api-qrcode.brasilcashbank.com/oauth/token</code></td></tr><tr><td>Content-Type</td><td><code>application/json</code></td></tr></tbody></table>

#### 3. Authentication Parameters

The following parameters must be included in the JSON request body:

<table><thead><tr><th width="133">Parameter</th><th width="176">Type</th><th width="116">Required</th><th>Description</th></tr></thead><tbody><tr><td>client_id</td><td>String</td><td>Yes</td><td>Application identifier.</td></tr><tr><td>cert_client</td><td>String</td><td>Yes</td><td>Client certificate credential encoded in Base64.</td></tr><tr><td>grant_type</td><td>String</td><td>Yes</td><td>Authentication grant type. Must be set to <code>credentials</code>.</td></tr></tbody></table>

#### 4. Certificate Encoding

Before sending the authentication request, the client certificate credential must be converted into Base64 format.

**Example:**

{% tabs %}
{% tab title="C#" %}

```c#
string cert_clientBase64 =
    Convert.ToBase64String(
        System.Text.Encoding.ASCII.GetBytes(
            "YOUR_CLIENT_CERTIFICATE"
        )
    );
```

{% endtab %}

{% tab title="Second Tab" %}

```javascript
const cert_clientBase64 = btoa("YOUR_CLIENT_CERTIFICATE");
```

{% endtab %}

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

```javascript
const cert_clientBase64 = Buffer
    .from("YOUR_CLIENT_CERTIFICATE", "ascii")
    .toString("base64");
```

{% endtab %}

{% tab title="PHP" %}

```php
$cert_clientBase64 = base64_encode(
    "YOUR_CLIENT_CERTIFICATE"
);
```

{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```python
import base64
cert_clientBase64 = base64.b64encode( "YOUR_CLIENT_CERTIFICATE".encode("ascii") ).decode("ascii")
```

{% endcode %}
{% endtab %}
{% endtabs %}

csharp

```
string cert_clientBase64 =
    Convert.ToBase64String(
        System.Text.Encoding.ASCII.GetBytes(
            "YOUR_CLIENT_CERTIFICATE"
        )
    );
```

The resulting Base64 string must be assigned to the `cert_client` property in the authentication request.

**Important:** The original certificate credential must be stored securely and must never be exposed in source code, public repositories, or application logs.

#### 5. Authentication Request

The application must send a POST request containing the authentication parameters in JSON format.

{% tabs %}
{% tab title="C#" %}

<pre class="language-csharp" data-expandable="true"><code class="lang-csharp"><strong>var client = new HttpClient();
</strong>
string cert_clientBase64 = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("0478e38a442c174a1d3f6f342fa6b8c2e02343e3cddfbf787d95bdf26c6214e3e1f0ce5da848841faf23b8c7c318e89e89a790583f35d196f9cc724de4fb8026fc"));

var request = new HttpRequestMessage(HttpMethod.Post, "https://api-qrcode.routepag.com/oauth/token");

var data = new {
    client_id = "kN3UMKvVdXqvq9eFPFXVA2ZMY3NKrW9c8z3q6LbcxNt",
    cert_client = cert_clientBase64,
    grant_type = "credentials"
};

var content = new StringContent(JsonConvert.SerializeObject(data), null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

</code></pre>

{% endtab %}

{% tab title="Javascript" %}
{% code expandable="true" %}

```javascript
const certClient = Buffer.from(
  "0478e38a442c174a1d3f6f342fa6b8c2e02343e3cddfbf787d95bdf26c6214e3e1f0ce5da848841faf23b8c7c318e89e89a790583f35d196f9cc724de4fb8026fc",
  "ascii"
).toString("base64");

const data = {
  client_id: "kN3UMKvVdXqvq9eFPFXVA2ZMY3NKrW9c8z3q6LbcxNt",
  cert_client: certClient,
  grant_type: "credentials"
};

const response = await fetch(
  "https://api-qrcode.routepag.com/oauth/token",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(data)
  }
);

if (!response.ok) {
  throw new Error(
    `HTTP ${response.status}: ${await response.text()}`
  );
}

const result = await response.text();

console.log(result);
```

{% endcode %}
{% endtab %}

{% tab title="Node.js (AXIOS)" %}
{% code expandable="true" %}

```typescript
const axios = require("axios");

const certClient = Buffer.from(
  "0478e38a442c174a1d3f6f342fa6b8c2e02343e3cddfbf787d95bdf26c6214e3e1f0ce5da848841faf23b8c7c318e89e89a790583f35d196f9cc724de4fb8026fc",
  "ascii"
).toString("base64");

const data = {
  client_id: "kN3UMKvVdXqvq9eFPFXVA2ZMY3NKrW9c8z3q6LbcxNt",
  cert_client: certClient,
  grant_type: "credentials"
};

try {
  const response = await axios.post(
    "https://api-qrcode.routepag.com/oauth/token",
    data,
    {
      headers: {
        "Content-Type": "application/json"
      }
    }
  );

  console.log(response.data);
} catch (error) {
  console.error(
    error.response?.data || error.message
  );
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code expandable="true" %}

```php
function getPayGoToken()
{
    $certClient = base64_encode(
        "0478e38a442c174a1d3f6f342fa6b8c2e02343e3cddfbf787d95bdf26c6214e3e1f0ce5da848841faf23b8c7c318e89e89a790583f35d196f9cc724de4fb8026fc"
    );

    $data = [
        "client_id"   => "kN3UMKvVdXqvq9eFPFXVA2ZMY3NKrW9c8z3q6LbcxNt",
        "cert_client" => $certClient,
        "grant_type"  => "credentials"
    ];

    $ch = curl_init(
        "https://api-qrcode.routepag.com/oauth/token"
    );

    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json",
            "Accept: application/json"
        ],
        CURLOPT_POSTFIELDS => json_encode($data),
    ]);

    $response = curl_exec($ch);

    if ($response === false) {
        throw new Exception(curl_error($ch));
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($httpCode < 200 || $httpCode >= 300) {
        throw new Exception(
            "PayGo retornou HTTP {$httpCode}: {$response}"
        );
    }

    return json_decode($response, true);
}

try {
    $token = getPayGoToken();

    echo $token["access_token"];

} catch (Exception $e) {
    echo "Erro: " . $e->getMessage();
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```python
import base64
import requests

# Certificado / credencial em Base64
cert_client = base64.b64encode(
    b"0478e38a442c174a1d3f6f342fa6b8c2e02343e3cddfbf787d95bdf26c6214e3e1f0ce5da848841faf23b8c7c318e89e89a790583f35d196f9cc724de4fb8026fc"
).decode("ascii")

data = {
    "client_id": "kN3UMKvVdXqvq9eFPFXVA2ZMY3NKrW9c8z3q6LbcxNt",
    "cert_client": cert_client,
    "grant_type": "credentials"
}

response = requests.post(
    "https://api-qrcode.routepag.com/oauth/token",
    json=data,
    headers={
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
)

response.raise_for_status()

print(response.text)
```

{% endcode %}
{% endtab %}
{% endtabs %}

**ATTENTION:** Monitor the requests and try making new token requests every **7200 SECONDS**.

#### 7. API Response

Upon successful authentication, the API is expected to return a JSON response containing authentication information.

The exact response structure must be confirmed using the actual API response or the official BrasilCashBank documentation.

The application should be prepared to handle fields such as:

| Field         | Description                                                     |
| ------------- | --------------------------------------------------------------- |
| access\_token | Authentication token used to authorize subsequent API requests. |
| token\_type   | Type of authentication token, if provided.                      |
| expires\_in   | Token validity period in seconds, if provided.                  |

Finally, if the API Key is not informed and/or is invalid, requests will issue a 403 error with the return:

<details>

<summary> 🟢 200 - S<strong>uccessfully</strong></summary>

{% code expandable="true" %}

```json
{
    "access_token": "TG9yanBkb0dHdlJVNEtWUDZHUzBUNXNJWHlqQzNuWXl3Y1M4d2hOamNqVGxSMWJaa29iNGJwUU1ZZFo0QlZ4R0kzTnhPY3cyOEZwc2lqZnhHb2h3QlBBeEtsaDk3eTdaVEN0cnYwcGdacTc4eG50bU9zSHhwWExzclp5YW4xY05qSm5nOGp6NHI1YTNER29QczJIMmg0OUxJVGp6OGF0dA==",
    "token_type": "Bearer",
    "expires_in": 7200
}
```

{% endcode %}

</details>

<details>

<summary>🔴400 - Bad request</summary>

</details>

<details>

<summary>🔴401 - Unauthorized</summary>

<pre class="language-javascript"><code class="lang-javascript"><strong>{ 
</strong>  "error": "Unathorized"
}
</code></pre>

</details>

<details>

<summary>🔴500 - Internal server error</summary>

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.brasilcashbank.com/pix-in/auth-token.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
