Azion `Client` interface
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, 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.
Contributors