Send data from JavaScript app to Axiom
This page explains how to send data from a JavaScript app to Axiom.
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:
| Capabilities | @axiomhq/js | @axiomhq/logging |
|---|---|---|
| Send data to Axiom | Yes | Yes |
| Query data | Yes | No |
| Capture errors | Yes | No |
| Create annotations | Yes | No |
| Transports | No | Yes |
| Structured logging by default | No | Yes |
| Send data to multiple places from a single function | No | Yes |
The @axiomhq/logging library is a logging solution that also serves as the base for other libraries like @axiomhq/react and @axiomhq/nextjs.
Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to ingest data to the dataset you have created.
Use @axiomhq/js
Install @axiomhq/js
In your terminal, go to the root folder of your JavaScript app and run the following command:
Configure authentication
Pass token to the Axiom constructor. @axiomhq/js doesn’t read environment variables automatically. To keep the token out of your source code, export AXIOM_TOKEN (for example, with eval $(axiom config export -f) using the Axiom CLI) and pass token: process.env.AXIOM_TOKEN:
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:
| Edge deployment | Base domain for ingest and query |
|---|---|
US East 1 (AWS) | us-east-1.aws.edge.axiom.co |
EU Central 1 (AWS) | eu-central-1.aws.edge.axiom.co |
For more information about edge deployments, see Edge deployments.
Client options
| Option | Required | Description |
|---|---|---|
token | yes | An Axiom API token with permissions for the operations you use. |
orgId | no | Organization ID. Required when using a personal token. |
edge | no | Edge domain for ingest and query, without scheme. Example: eu-central-1.aws.edge.axiom.co. Use this to target a region. |
edgeUrl | no | Full edge URL with scheme. Takes precedence over edge if both are set. Useful for self-hosted or proxy setups. |
url | no | Base URL for non-ingest API operations. Only needed if you call other Axiom APIs from the same client. |
onError | no | Callback invoked when sending data fails. Defaults to console.error. |
fetch | no | Custom fetch implementation used for all HTTP requests. Useful for routing through a proxy such as undici.ProxyAgent. |
axiomClient | no | Additional space-separated product/version tokens appended to the X-Axiom-Client header. |
The client also exposes service namespaces for Axiom resources: datasets (including metrics metadata), dashboards, savedQueries, and users for read operations, monitors for full CRUD operations, and annotations.
Send data to Axiom
The following example sends data to Axiom:
The client automatically batches events in the background. In most cases, call flush() only before your app exits.
Query data
The following example queries data from Axiom:
Starting in @axiomhq/js 2.0.0, APL queries return tabular results by default. Iterate rows with res.tables[0].events(), or pass { format: 'legacy' } as the second argument to axiom.query for the previous response shape.
For more examples, see the examples in GitHub.
Query metrics
To query metrics, set type to mpl and specify a time range. The following example queries the go.memory.used metric over the last hour. Set edge to the region for your dataset.
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.typescript -
AxiomJSTransport: Sends logs to Axiom using the @axiomhq/js library.typescript -
ProxyTransport: Sends logs to the proxy server function that acts as a proxy between your app and Axiom. It’s particularly useful when your app runs on top of a server-enabled framework like Next.js or Remix.typescript -
SimpleFetchTransport: Sends batched JSON log payloads to any HTTP endpoint usingfetch. It holds up to 1,000 queued events and retries on HTTP429and5xxresponses.typescript
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. Default forlogLevel.warn: Warning logs.error: Error logs.off: Disables logging.
Set logLevel on new Logger({ transports: [...], logLevel: 'warn' }) to filter logs before they reach transports.
Formatters
Formatters change the content of a log before sending it to a transport. Set overrideDefaultFormatters: true on the Logger config to replace the built-in formatters instead of running after them. For example:
Schema validation
Validate structured log fields with any Standard Schema library (such as Zod, Valibot, or ArkType) using schema, outputSchema, and onValidationError in Logger:
Related logging options
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: