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.
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:
import { createClient } from 'azion';
// Instantiate the clientconst client = createClient({ token: 'your-api-token', debug: true });
// Access the SQL module and create a Databaseconst { 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:
import { createClient } from 'azion';import type { AzionClient } from 'azion/client';import type { AzionDatabaseResponse } from 'azion/sql';// Instantiate the clientconst client = createClient({ token: 'your-api-token', debug: true });
// Access the SQL module and create a Databaseconst { 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:
import { createClient, StorageClient } from 'azion/storage';
// Create a client for the Storage moduleconst 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.
import { createDatabase } from 'azion/sql';
// Call the function createDatabase directly from its packageconst { 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:
import { createClient } from 'azion';
const client = createClient({ token: 'your-api-token', debug: true });
// Access the KV moduleconst kvClient = await client.kv .on('error', (err) => console.error('KV Error:', err)) .connect();
// Store a valueawait kvClient.set('user:123', JSON.stringify({ name: 'John' }), { expiration: { type: 'EX', value: 3600 }, // 1 hour TTL});
// Retrieve a valueconst userData = await kvClient.get('user:123');console.log('User data:', userData);
// Hash operationsawait kvClient.hSet('config', 'theme', 'dark');const config = await kvClient.hGetAll('config');
// Disconnect when doneawait kvClient.disconnect();For more details on KV operations, see the KV library documentation.