# Authentication

All of W3block API services shares the same authentication that can be obtained using ID services. Basically, ID controls all of users, companies and permissions and besides that, it can generate JWT tokens that can be used to authenticate requests into our entire ecosystem. Above you can see our authentication flow.

<figure><img src="/files/guvysCCoOBbTZMxADMcw" alt=""><figcaption><p>W3block authentication flow</p></figcaption></figure>

## How to Authenticate

To authenticate, you must use the ID services API endpoint above:

{% openapi src="<https://api-id.pixway.io/sdk-json>" path="/auth/signin" method="post" %}
<https://api-id.pixway.io/sdk-json>
{% endopenapi %}

You can also use our [node-js ID SDK](https://www.npmjs.com/package/@w3block/sdk-id) to authenticate.

### Authenticating as user

If you want to authenticate some user registered in your tenant base, you can use the following sdk code:

{% code lineNumbers="true" %}

```typescript
import { W3blockIdSDK } from '@w3block/sdk-id';
const idSdk = new W3blockIdSDK({
    baseURL: 'https://api-id.pixway.io',
    autoRefresh: true,
});
await idSdk.authenticate({
    email: 'test@w3block.id',
    password: '<change.me>',
    tenantId: '<your tenant id (also known as your company id)>',
});
const jwtToken = idSdk.getAuthToken();
console.log(`My authentication token id: ${jwtToken}`);
```

{% endcode %}

### Authenticating as tenant

Sometimes you want to integrate some application into our service. This way, is not common to authenticate as a common user, so you want to authenticate as a tenant or basically using a tenant api key and secret. Above you can see some example on how to authenticate using this method.

```typescript
import { W3blockIdSDK } from '@w3block/sdk-id';
const idSdk = new W3blockIdSDK({
    baseURL: 'https://api-id.pixway.io',
    autoRefresh: true,
});
await idSdk.authenticate({
    key: '<your tenant key>',
    secret: '<your tenant secret>',
    tenantId: '<your tenant id (also known as your company id)>',
});
const jwtToken = idSdk.getAuthToken();
console.log(`My authentication token id: ${jwtToken}`);
```

## Using the authentication token in our api services

To be authenticated into our api services, you must pass the obtained jwt token into the **Authorization** request header using the pattern of bearer tokens

{% hint style="info" %}
**Example**:

Authorization: Bearer \<JWT token>
{% endhint %}

You can also directly authenticate into our services using the tenant credentials without getting the tenant jwt token. To do that, you just need to inform in the request headers the following params:

* **`x-w3block-id`**: Your tenant id
* **`x-w3block-api-key`**: Your tenant api key
* **`x-w3block-secret`**: Your tenant api secret

This way, the api service that you're calling will automatically handle the tenant authentication without the need of manually get the JWT token as example above.


---

# 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://w3block.gitbook.io/w3block-eng/dev-docs/api/authentication.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.
