Azion `Domains` library

Azion Domains library provides a simple interface to interact with the Edge Domains API, allowing you to create, list, get, update, and delete domains.

You can interact with the API using a client or calling the methods directly from the library. When making direct calls, you can use the environment variables to configure the client without passing the token and debug parameters directly.

This is an example of how a .env file with your environment variables may look like:

Go to Azion Libraries Overview
Terminal window
AZION_TOKEN=<your-api-token>
AZION_DEBUG=true
VariableDescription
AZION_TOKENYour Azion API token.
AZION_DEBUGEnable debug mode (true/false).

If you want to create a specific client for interacting with Domains, create one by calling the createClient method from the library:

import { createClient } from 'azion/domains';
import type { AzionDomain, AzionDomainsClient } from 'azion/domains';
const client: AzionDomainsClient = createClient({ token: 'your-api-token', { debug: true } });
const { data: newDomain, error }: AzionDomainsResponse<AzionDomain> = await client.createDomain({ name: 'example domain', edgeApplicationId: 123 });
if (newDomain) {
console.log(`Domain created with ID: ${newDomain.id}`);
}

The createClient method has the following parameters and return value:

Parameters:

ParameterTypeDescription
configPartial<{ token: string; options?: OptionsParams }>Configuration options for the Domain client.

Returns:

Return TypeDescription
AzionDomainsClientAn object with methods to interact with Domains.

Usage

createDomain

This method creates a new domain. For example:

import { createDomain } from 'azion/domains';
import type { AzionDomain, AzionDomainsResponse } from 'azion/domains';
const { data: domain, error }: AzionDomainsResponse<AzionDomain> = await createDomain({
name: 'example domain',
edgeApplicationId: 123,
});
if (domain) {
console.log(`Domain created with ID: ${domain.id}`);
} else {
console.error('Failed to create domain', error);
}

Parameters:

ParameterTypeDescription
domainAzionCreateDomainThe domain object containing domain configuration.
options{ debug?: boolean }Optional. The object containing a debug flag to enable detailed logging.

Returns:

Return TypeDescription
Promise<AzionDomainsResponse<AzionDomain>>The created domain object or error if failed.

getDomains

This method lists all domains. Example:

import { getDomains } from 'azion/domains';
import type { AzionDomainCollection, AzionDomainsResponse } from 'azion/domains';
const { data: domains, error }: AzionDomainsResponse<AzionDomainCollection> = await getDomains();
if (domains) {
console.log(`Found ${domains.count} domains`);
} else {
console.error('Failed to list domains', error);
}

Parameters:

ParameterTypeDescription
options{ debug?: boolean }Optional. The object containing a debug flag to enable detailed logging.
queryParams{ pageSize?: number; page?: number; order?: 'id' | 'name'; sort?: 'asc' | 'desc' }Optional. Query parameters for pagination and sorting.

Returns:

Return TypeDescription
Promise<AzionDomainsResponse<AzionDomainCollection>>An array of domain objects or error if failed.

getDomain

Use this method to get a domain by its ID.

import { getDomain } from 'azion/domains';
import type { AzionDomain, AzionDomainsResponse } from 'azion/domains';
const domainId = 123;
const { data: domain, error }: AzionDomainsResponse<AzionDomain> = await getDomain(domainId);
if (domain) {
console.log(`Found domain with name: ${domain.name}`);
} else {
console.error('Failed to get domain', error);
}

Parameters:

ParameterTypeDescription
domainIdnumberThe domain ID.
options{ debug?: boolean }Optional. The object containing a debug flag to enable detailed logging.

updateDomain

This method updates a domain.

import { updateDomain } from 'azion/domains';
import type { AzionDomain, AzionDomainsResponse } from 'azion/domains';
const domainId = 123;
const { data: domain, error }: AzionDomainsResponse<AzionDomain> = await updateDomain(domainId, {
name: 'new domain name',
edgeApplicationId: 456,
});
if (domain) {
console.log(`Updated domain with name: ${domain.name}`);
} else {
console.error('Failed to update domain', error);
}

Parameters:

ParameterTypeDescription
domainIdnumberThe domain ID.
domainAzionUpdateDomainThe domain object to update.
options{ debug?: boolean }Optional. The object containing a debug flag to enable detailed logging.

Returns:

TypeDescription
Promise<AzionDomainsResponse<AzionDomain>>The updated domain object or state failed.

deleteDomain

This method deletes a domain specified by its ID.

import { deleteDomain } from 'azion/domains';
import type { AzionDomain, AzionDomainsResponse } from 'azion/domains';
const domainId = 123;
const { data: deletedDomain, error }: AzionDomainsResponse<AzionDomain> = await deleteDomain(domainId);
if (deletedDomain) {
console.log(`Deleted domain with ID: ${deletedDomain.id}`);
} else {
console.error('Failed to delete domain', error);
}

Parameters:

ParameterTypeDescription
domainIdnumberThe domain ID.
options{ debug?: boolean }Optional. The object containing a debug flag to enable detailed logging.

Returns:

TypeDescription
Promise<AzionDomainsResponse<AzionDeletedDomain>>The deleted domain id or state failed.

Types

If you’re using TypeScript, you’ll need to import and use the appropriate types for the parameters and return values. Here you can see a list of all the types used by the Domains library:

AzionDomainsClient

Configuration options for the Azion Domains client.

ParameterTypeDescription
tokenstringOptional. Your Azion API token.
optionsOptionsParamsOptional. Object options params.
debugbooleanOptional. Enable debug mode for detailed logging.

AzionDomainsResponse<T>

Generic response type returned by all Domains methods.

ParameterTypeDescription
dataTOptional. The response data.
error{ message: string; operation: string; }Optional. The error object.

AzionDomain

Represents a domain configuration with its properties.

ParameterTypeDescription
state'pending' | 'executed' | 'failed'The state of the domain.
idnumberOptional. The domain ID.
namestringThe domain name.
urlstringOptional. The domain URL.
environmentstringOptional. The domain environment.
edgeApplicationIdnumberThe edge application ID.
activebooleanThe domain status.
cnameAccessOnlybooleanOptional. CNAME access only.
cnamesstring[]List of CNAMEs.
edgeFirewallIdnumberOptional. The edge firewall ID.
digitalCertificateIdnumber | string | nullthe digital certificate ID.
mtlsobject | nullOptional. Mutual TLS configuration.
mtls.verificationstringVerification method: enforce or permissive.
mtls.trustedCaCertificateIdnumberThe trusted CA certificate ID.
mtls.crlListnumber[]The list of CRL IDs.

AzionDomainCollection

Represents a collection of domains with their current state and pagination details.

ParameterTypeDescription
state'pending' | 'executed' | 'failed'The state of the domain list.
pagesnumberNumber of pages.
countnumberNumber of domains.
resultsAzionDomain[]Array of domain objects.

AzionDeleteDomain

Represents the parameters required to delete a domain.

ParameterTypeDescription
idnumberThe domain ID.
state'pending' | 'executed' | 'failed'The state of the domain deletion.

Contributors