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
.
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.
The @axiomhq/logging library is currently in public preview. For more information, see Features states.
Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to update 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 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:
Send data to Axiom
The following example sends data to Axiom:
Replace DATASET_NAME
with the name of the Axiom dataset where you want to send 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 want to send 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:
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 fields of a log before sending it to a transport. For example:
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:
Was this page helpful?