# Client

The `azion` package carries the Azion Lib client. Its `createClient` function returns one object that holds a client for each of six modules: Storage, SQL, Purge, Domains, Applications, and AI. You pass your token once, to `createClient`. The `azion` package receives bug fixes only, and its maintenance ends in December 2026.

Install the package:

```bash
npm install azion
```

The samples on this page are JavaScript and TypeScript ES modules that use top-level `await`, and they run in Node.js. The TypeScript samples import types with `import type`, which keeps them loadable when the type annotations are stripped.

---

## Authentication

The client takes your [personal token](/en/documentation/fundamentals/personal-tokens/) in the `token` field of [createClient](#createclient). A function called directly from a module package, without a client, reads the token from the `AZION_TOKEN` environment variable instead.

For how the Azion Lib packages resolve the token and the debug setting, refer to [How Azion Lib works](/en/documentation/devtools/azion-lib/how-it-works/).

---

## createClient

Creates the client. `createClient` is also the default export of the `azion` package.

```typescript
function createClient(config?: {
  token?: string;
  options?: AzionClientOptions;
}): AzionClient;
```

| Parameter | Type                                        | Required | Description                                                                                                                       |
| --------- | ------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `token`   | `string`                                    | No       | Your Azion personal token.                                                                                                        |
| `options` | [`AzionClientOptions`](#azionclientoptions) | No       | Request options. The type accepts `debug` only inside `options`: a top-level `debug` field is ignored, and TypeScript refuses it. |

Returns an [AzionClient](#azionclient), with one property per module. Each property is that module's client, and its methods return the response envelope the module page documents.

This sample creates a client and an SQL database through its `sql` module:

```javascript
import { createClient } from 'azion';

// Instantiate the client
const client = createClient({ token: process.env.AZION_TOKEN, options: { debug: false } });

// Access the SQL module and create a Database
const { data: newDatabase, error } = await client.sql.createDatabase('my-database');
if (newDatabase) {
  console.log(`Database created with ID: ${newDatabase.id}`);
} else {
  console.error('Failed to create database', error);
}
```

Output:

```text
Database created with ID: 1866
```

In TypeScript, import `AzionClient` from the `azion` root and the module types from the module subpath. This sample reads the database that the previous call created:

```typescript
import { createClient } from 'azion';
import type { AzionClient } from 'azion';
import type { AzionDatabaseResponse, AzionDatabase } from 'azion/sql';

// Instantiate the client
const client: AzionClient = createClient({ token: process.env.AZION_TOKEN, options: { debug: false } });

// Access the SQL module and read a Database
const { data: database, error }: AzionDatabaseResponse<AzionDatabase> = await client.sql.getDatabase('my-database');
if (database) {
  console.log(`Database ${database.name} has ID ${database.id} (${database.status})`);
} else {
  console.error('Failed to get database', error);
}
```

Output:

```text
Database my-database has ID 1866 (created)
```

---

## Modules

An [AzionClient](#azionclient) holds exactly six properties, one per module. Each property takes the same calls as the client that the module's own package creates, and each module page documents those calls and their response envelope.

| Property       | Type                      | What it reaches                                                                                             | Module page                                                       |
| -------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| `storage`      | `AzionStorageClient`      | Object Storage buckets, and their objects through the methods each bucket carries.                          | [Storage](/en/documentation/devtools/azion-lib/storage/)          |
| `sql`          | `AzionSQLClient`          | SQL databases: create, list, read, and delete. A database it returns carries `query` and `execute` methods. | [SQL](/en/documentation/devtools/azion-lib/sql/)                  |
| `purge`        | `AzionPurgeClient`        | Cache purges by URL, cache key, or wildcard.                                                                | [Purge](/en/documentation/devtools/azion-lib/purge/)              |
| `domains`      | `AzionDomainsClient`      | Domains: create, list, read, update, and delete. Calls Azion API v3.                                        | [Domains](/en/documentation/devtools/azion-lib/domains/)          |
| `applications` | `AzionApplicationsClient` | Applications: create, list, read, update, and delete. Calls Azion API v3.                                   | [Applications](/en/documentation/devtools/azion-lib/application/) |
| `ai`           | `AzionAIClient`           | Chat completions, with `chat` and `streamChat`.                                                             | [AI client](/en/documentation/devtools/azion-lib/ai-client/)      |

---

## Module clients and standalone functions

The aggregate client is one of three ways to call a module. Each module also has its own internal client, which its package creates with its own `createClient`, and each module function can be called on its own, without any client. A module client takes the token in its `token` field, like the aggregate client. A standalone function reads the token from the `AZION_TOKEN` environment variable, for example from a `.env` file.

The Storage and SQL modules have their own packages, `@aziontech/storage` and `@aziontech/sql`. Install them:

```bash
npm install @aziontech/storage @aziontech/sql
```

This sample creates a Storage module client from `@aziontech/storage` and creates a bucket with it:

```typescript
import { createClient } from '@aziontech/storage';
import type { AzionStorageClient, AzionStorageResponse, AzionBucket } from '@aziontech/storage';

// Create a client for the Storage module
const client: AzionStorageClient = createClient({ token: process.env.AZION_TOKEN, options: { debug: false } });

const { data, error }: AzionStorageResponse<AzionBucket> = await client.createBucket({
  name: 'my-bucket',
  workloads_access: 'read_only',
});

if (data) {
  console.log(`Bucket created with name: ${data.name}`);
} else {
  console.error('Failed to create bucket', error);
}
```

Output:

```text
Bucket created with name: my-bucket
```

This sample calls `createDatabase` from `@aziontech/sql` with no client. The token comes from `AZION_TOKEN`, and the second argument carries the request options:

```javascript
import { createDatabase } from '@aziontech/sql';

// Call the function createDatabase directly from its package
const { data, error } = await createDatabase('my-new-database', { debug: false });
if (data) {
  console.log(`Database created with ID: ${data.id}`);
} else {
  console.error('Failed to create database', error);
}
```

Output:

```text
Database created with ID: 1867
```

Use the aggregate client to reach several modules from one object. Call a standalone function when you configure the token and the options through environment variables and want only the function you call.

---

## Types

The `azion` root exports the `AzionClient` and `AzionClientConfig` types. Import them with `import type`. The root also exports `defineConfig`, `processConfig`, and `convertJsonConfigToObject`, which the [Config](/en/documentation/devtools/azion-lib/config/) page documents.

### AzionClient

The client that [createClient](#createclient) returns. For what each property reaches, refer to [Modules](#modules).

| Property       | Type                      | Required | Description                     |
| -------------- | ------------------------- | -------- | ------------------------------- |
| `storage`      | `AzionStorageClient`      | Yes      | The Storage module client.      |
| `sql`          | `AzionSQLClient`          | Yes      | The SQL module client.          |
| `purge`        | `AzionPurgeClient`        | Yes      | The Purge module client.        |
| `domains`      | `AzionDomainsClient`      | Yes      | The Domains module client.      |
| `applications` | `AzionApplicationsClient` | Yes      | The Applications module client. |
| `ai`           | `AzionAIClient`           | Yes      | The AI module client.           |

### AzionClientConfig

The object that [createClient](#createclient) takes.

| Property  | Type                                        | Required | Description                |
| --------- | ------------------------------------------- | -------- | -------------------------- |
| `token`   | `string`                                    | No       | Your Azion personal token. |
| `options` | [`AzionClientOptions`](#azionclientoptions) | No       | Request options.           |

### AzionClientOptions

The request options type of `AzionClientConfig`. The `azion` root does not export it: the client declares it from `azion/sql`, so import it from there when you need it.

```typescript
type AzionClientOptions = {
  debug?: boolean;
  force?: boolean;
  env?: AzionEnvironment;
  external?: boolean;
};
```

| Property   | Type               | Required | Description                                                                       |
| ---------- | ------------------ | -------- | --------------------------------------------------------------------------------- |
| `debug`    | `boolean`          | No       | Turns on debug mode.                                                              |
| `force`    | `boolean`          | No       | Forces the operation, even when it can destroy data.                              |
| `env`      | `AzionEnvironment` | No       | The environment the calls go to: `'development'`, `'staging'`, or `'production'`. |
| `external` | `boolean`          | No       | Forces the REST API instead of the API built into the runtime.                    |

---

## Related resources

- [Azion Lib](/en/documentation/devtools/azion-lib.md): The libraries Azion Lib ships and the package that carries each one.
- [How Azion Lib works](/en/documentation/devtools/azion-lib/how-it-works.md): How the packages resolve the token and options, and where their functions run.
- [Storage](/en/documentation/devtools/azion-lib/storage.md): Every bucket and object function the storage module client exposes.
- [SQL](/en/documentation/devtools/azion-lib/sql.md): Every database and query function the sql module client exposes.
