# Azion `Client` interface

import DocButton from '~/components/webkit/DocButton.vue';

The Azion **Client** interface provides a unified interface to interact with all products and services. You can use the `client` to access and manage all products and functionalities across Storage, SQL, Purge, KV, Domains, and more. 

When instantiating a client, you can define configurations such as `token` and `debug` explicitly as parameters. You can then interact with Azion functionalities directly through the `client`, in a simplified and centralized way.

<DocButton href="/en/documentation/products/azion-lib/overview/" label="Go to Azion Libraries Overview" kind="secondary" size="medium" />

---

## Using Azion Client

To use the **Client** interface, you must import the `createClient` function from **Azion Lib**. You can then pass your `token` as a parameter and use the instance of `client` to access modules and their functionalities.

The example below shows an implementation in **JavaScript**:

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

// Instantiate the client
const client = createClient({ token: 'your-api-token', debug: true });

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

If you're using TypeScript, import the appropriate types as in the example below:

```ts
import { createClient } from 'azion';
import type { AzionClient } from 'azion/client';
import type { AzionDatabaseResponse } from 'azion/sql';
// Instantiate the client
const client = createClient({ token: 'your-api-token', debug: true });

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

---

## Azion Client vs independent package Functions

Alternatively, if you prefer to use individual functions directly from each package, you need to configure tokens and settings via environment variables (for example, using a `.env` file). 

Each module has its own internal client that manages the interactions. The following example shows the usage of a client for a specific module:

```js
import { createClient, StorageClient } from 'azion/storage';

// Create a client for the Storage module
const client: StorageClient = createClient({ token: 'your-api-token', debug: true });

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

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

It's also possible to use specific functions directly from their packages, without using a `client`, as shown in the example below.

:::note
When using individual functions without a `client`, you need to configure tokens and settings via environment variables (for example, using a `.env` file).
:::

```js
import { createDatabase } from 'azion/sql';

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

This flexibility allows you to either manage everything through the client for simplicity or call specific functions from each package with more control over environment configurations.

---

## Available modules

The Azion Client provides access to the following modules:

| Module | Description |
|--------|-------------|
| `storage` | Manage buckets and objects in Object Storage. |
| `sql` | Create and query SQL databases. |
| `purge` | Purge cache by URL, cache key, or wildcard. |
| `domains` | Manage domains and their configurations. |
| `kv` | Interact with KV Store using a Redis-like API. |

### Using the KV module

The `kv` module provides a Redis-like interface for key-value operations:

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

const client = createClient({ token: 'your-api-token', debug: true });

// Access the KV module
const kvClient = await client.kv
  .on('error', (err) => console.error('KV Error:', err))
  .connect();

// Store a value
await kvClient.set('user:123', JSON.stringify({ name: 'John' }), {
  expiration: { type: 'EX', value: 3600 }, // 1 hour TTL
});

// Retrieve a value
const userData = await kvClient.get('user:123');
console.log('User data:', userData);

// Hash operations
await kvClient.hSet('config', 'theme', 'dark');
const config = await kvClient.hGetAll('config');

// Disconnect when done
await kvClient.disconnect();
```

For more details on KV operations, see the [KV library documentation](/en/documentation/products/azion-lib/kv/).