# Access Token generation

To generate a Token, you will need a *Project ID* and *Project Secret,* which are available after activating the Project in [**maibmerchants**](https://maibmerchants.md).

For the Test Project, the *Project ID* and *Project Secret* will be available immediately after the settings are made (IP, Platform, Callback URL, Ok URL, Fail URL).

**Flow authentication by Access Token:**

1. Send a request to the token generation endpoint using the *Project ID* and *Project Secret*. If the transmitted data is valid, you will receive a Token (Token lifetime) and a Refresh Token (Refresh Token lifetime) in response.
2. If the token has expired, use Refresh Token to generate a new Access Token. If the Refresh Token has also expired, use the *Project ID* and *Project Secret* (see point 1).
3. Make requests to **maib ecomm** using the Access Token.

### Token generation using Project ID and Project Secret

| API endpoint (POST)                              |
| ------------------------------------------------ |
| <https://api.maibmerchants.md/v1/generate-token> |

**Request parameters (body)**

<table><thead><tr><th width="164">Parameter</th><th width="111">Required</th><th width="88">Type</th><th>Description</th></tr></thead><tbody><tr><td>projectId</td><td>YES</td><td>string</td><td>Project ID from Project in <em><strong>maibmerchants</strong></em></td></tr><tr><td>projectSecret</td><td>YES</td><td>string</td><td>Project Secret from Project in <em><strong>maibmerchants</strong></em></td></tr></tbody></table>

**Request example**

```bash
curl --location --request POST "https://api.maibmerchants.md/v1/generate-token" \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "projectId": "8508706",
    "projectSecret": "60462913-da44-47fa-8c82-146b676729b9"
    }'
```

**Response parameters**

<table><thead><tr><th width="211.33333333333331">Parameter</th><th width="135">Type</th><th>Description</th></tr></thead><tbody><tr><td>result</td><td>Object</td><td>An object containing the Token and the Refresh Token.</td></tr><tr><td><ul><li>accessToken</li></ul></td><td>string</td><td>Access Token</td></tr><tr><td><ul><li>expiresIn</li></ul></td><td>integer</td><td>Access Token lifetime in seconds</td></tr><tr><td><ul><li>refreshToken</li></ul></td><td>string</td><td>Refresh Token for generating a new Access Token.</td></tr><tr><td><ul><li>refreshExpiresIn</li></ul></td><td>integer</td><td>Refresh Token lifetime in seconds</td></tr><tr><td><ul><li>tokenType</li></ul></td><td>string</td><td>Token type (<em>Bearer</em>)</td></tr><tr><td>ok</td><td>Boolean</td><td><p>Request processing status.</p><p><em>true</em> - no errors</p><p><em>false -</em> an error occurred (error details will be in <em>errors)</em></p></td></tr><tr><td>errors</td><td>Array</td><td>Request processing errors. <a href="errors/api-errors"><mark style="color:blue;">Errors table</mark></a></td></tr><tr><td><ul><li>errorCode</li></ul></td><td>String</td><td>Error code</td></tr><tr><td><ul><li>errorMessage</li></ul></td><td>String</td><td>Error description</td></tr><tr><td><ul><li>errorArgs</li></ul></td><td>Object</td><td>Object contains parameters with error details</td></tr></tbody></table>

**Response example**

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

```json
{
  "result": {
    "accessToken": "xxxxxx",
    "expiresIn": 300,
    "refreshToken": "xxxxxx",
    "refreshExpiresIn": 1800,
    "tokenType": "Bearer"
  },
  "ok": true
}
```

{% endtab %}

{% tab title="Error" %}

```json
{
    "errors": [
        {
            "errorCode": "11001",
            "errorMessage": "Invalid credentials. Please check 'projectId' and 'projectSecret' parameters"
        }
    ],
    "ok": false
}
```

{% endtab %}
{% endtabs %}

### Token generation using Refresh Token

**Request parameter (body)**

<table><thead><tr><th width="162">Parameter</th><th width="115">Required</th><th width="103">Type</th><th>Description</th></tr></thead><tbody><tr><td>refreshToken</td><td>Yes</td><td>string</td><td>Refresh Token</td></tr></tbody></table>

**Request example**

```bash
curl --location --request POST "https://api.maibmerchants.md/v1/generate-token" \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "refreshToken": "xxxxxxxxxxxxx"
    }'
```

**Response example**

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

```json
"result": {
    "accessToken": "xxxxxx",
    "expiresIn": 300,
    "refreshToken": "xxxxxx",
    "refreshExpiresIn": 1800,
    "tokenType": "Bearer"
  },
  "ok": true
}
```

{% endtab %}

{% tab title="Error" %}

```json
{
    "errors": [
        {
            "errorCode": "11002",
            "errorMessage": "Invalid or expired 'refreshToken' parameter"
        }
    ],
    "ok": false
}
```

{% endtab %}
{% endtabs %}

### Example of Authentication via Access Token

```bash
curl -X 'POST' \
  'https://api.maibmerchants.md/v1/pay' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer access_token' \
  -H 'Content-Type: application/json' \
  -d '{
  "clientIp": "135.250.245.121",
  "amount": 10.25,
  "currency": "USD",
  "description": "Description",
  "language": "en",
  "orderId": "123",
  "clientName": "Client Name",
  "email": "customer@gmail.com",
  "phone": "069123456",
  "delivery": 1.25,
  "items": [
    {
      "id": "123",
      "name": "Item",
      "price": 2.5,
      "quantity": 2
    }
  ],
  "callbackUrl": "https://example.com/callback",
  "okUrl": "https://example.com/ok",
  "failUrl": "https://example.com/fail"
}'
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.maibmerchants.md/e-commerce/access-token-generation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
