Skip to main content
JavaScript is a versatile, high-level programming language primarily used for creating dynamic and interactive web content. To send data from a JavaScript app to Axiom, use one of the following libraries of the Axiom JavaScript SDK: The choice between these options depends on your individual requirements: The @axiomhq/logging library is a logging solution that also serves as the base for other libraries like @axiomhq/react and @axiomhq/nextjs.
The @axiomhq/js and the @axiomhq/logging libraries are part of the Axiom JavaScript SDK, an open-source project and welcomes your contributions. For more information, see the GitHub repository.

Prerequisites

Use @axiomhq/js

Install @axiomhq/js

In your terminal, go to the root folder of your JavaScript app and run the following command:

Configure environment variables

Configure the environment variables in one of the following ways:
  • Export the API token as AXIOM_TOKEN.
  • Pass the API token to the constructor of the client:
  • Install the Axiom CLI, and then run the following command:

Configure region

By default, the client sends data to api.axiom.co. To target a specific edge region, set the edge option to the edge domain that matches the region your dataset lives in:
The following edge domains are available: For more information about edge deployments, see Edge deployments.
Always use the edge option to target a region. Don’t put a regional hostname in urlurl is reserved for non-ingest API operations and won’t route ingest correctly.
Edge endpoints require an API token (xaat-). Personal tokens (xapt-) are deprecated and aren’t supported for edge routing.

Client options

Send data to Axiom

The following example sends data to Axiom:
Replace DATASET_NAME with the name of the Axiom dataset where you send your data.
The client automatically batches events in the background. In most cases, call flush() only before your application exits.

Query data

The following example queries data from Axiom:
Replace DATASET_NAME with the name of the Axiom dataset where you send your data.
For more examples, see the examples in GitHub.

Capture errors

To capture errors, pass a method onError to the client:
By default, onError is set to console.error.

Create annotations

The following example creates an annotation:

Use @axiomhq/logging

Install @axiomhq/logging

In your terminal, go to the root folder of your JavaScript app and run the following command:

Send data to Axiom

The following example sends data to Axiom:
Because AxiomJSTransport sends data through the @axiomhq/js client, set the region by passing the edge option to the Axiom constructor. For the full list of edge domains, see Configure region.

Transports

The @axiomhq/logging library includes the following transports:
  • ConsoleTransport: Logs to the console.
  • AxiomJSTransport: Sends logs to Axiom using the @axiomhq/js library.
  • ProxyTransport: Sends logs the proxy server function that acts as a proxy between your application and Axiom. It’s particularly useful when your application runs on top of a server-enabled framework like Next.js or Remix.
Alternatively, create your own transports by implementing the Transport interface:

Logging levels

The @axiomhq/logging library includes the following logging levels:
  • debug: Debug-level logs.
  • info: Informational logs.
  • warn: Warning logs.
  • error: Error logs.

Formatters

Formatters are used to change the content of a log before sending it to a transport. For example:

Send data from JavaScript libraries and frameworks

To send data to Axiom from JavaScript libraries and frameworks, see the following:

Send data from Node.js

While the Axiom JavaScript SDK works on both the backend and the browsers, Axiom provides transports for some of the popular loggers: