This page explains how to send data from your Next.js app to Axiom.
Next.js is a popular open-source JavaScript framework built on top of React, developed by Vercel. It’s used by a wide range of companies and organizations, from startups to large enterprises, due to its performance benefits and developer-friendly features.
To send data from your Next.js app to Axiom, choose one of the following options:
The @axiomhq/nextjs library is currently in public preview. For more information, see Features states.
The choice between these options depends on your individual requirements:
The two options can collect different event types.
Event type | Axiom Vercel app | next-axiom library | @axiomhq/nextjs library |
---|---|---|---|
Application logs | Yes | Yes | Yes |
Web Vitals | No | Yes | Yes |
HTTP logs | Yes | Soon | Yes |
Build logs | Yes | No | No |
Tracing | Yes | No | Yes |
If you already use Vercel for deployments, the Axiom Vercel app can be easier to integrate into your existing experience.
The cost of these options can differ widely depending on the volume of data you transfer. The Axiom Vercel app depends on Vercel Log Drains, a feature that’s only available on paid plans. For more information, see the blog post on the changes to Vercel Log Drains.
For information on the Axiom Vercel app and migrating from the Vercel app to the next-axiom library, see Axiom Vercel app.
The rest of this page explains how to send data from your Next.js app to Axiom using the next-axiom or the @axiomhq/nextjs library.
The next-axiom library is an open-source project and welcomes your contributions. For more information, see the GitHub repository.
In your terminal, go to the root folder of your Next.js app and run the following command:
Add the following environment variables to your Next.js app:
NEXT_PUBLIC_AXIOM_DATASET
is the name of the Axiom dataset where you want to send data.NEXT_PUBLIC_AXIOM_TOKEN
is the Axiom API token you have generated.In the next.config.ts
file, wrap your Next.js configuration in withAxiom
:
To capture traffic requests, create a middleware.ts
file in the root folder of your Next.js app:
To send Web Vitals to Axiom, add the AxiomWebVitals
component from next-axiom to the app/layout.tsx
file:
Web Vitals are only sent from production deployments.
Send logs to Axiom from different parts of your app. Each log function call takes a message and an optional fields
object.
Wrap your route handlers in withAxiom
to add a logger to your request and log exceptions automatically:
To send logs from client components, add useLogger
from next-axiom to your component:
To send logs from server components, add Logger
from next-axiom to your component, and call flush before returning:
The log level defines the lowest level of logs sent to Axiom. Choose one of the following levels (from lowest to highest):
debug
is the default setting. It means that you send all logs to Axiom.info
warn
error
means that you only send the highest-level logs to Axiom.off
means that you don’t send any logs to Axiom.For example, to send all logs except for debug logs to Axiom:
To capture routing errors, use the error handling mechanism of Next.js:
app
folder.error.tsx
file.useLogger
from next-axiom to send the error to Axiom. For example:To extend the logger, use log.with
to create an intermediate logger. For example:
The @axiomhq/nextjs library is part of the Axiom JavaScript SDK, an open-source project and welcomes your contributions. For more information, see the GitHub repository.
In your terminal, go to the root folder of your Next.js app and run the following command:
Create the folder lib/axiom
to store configurations for Axiom.
Create a axiom.ts
file in the lib/axiom
folder with the following content:
In the lib/axiom
folder, create a server.ts
file with the following content:
The createAxiomRouteHandler
is a builder function that returns a wrapper for your route handlers. The wrapper handles successful responses and errors thrown within the route handler. For more information on the logger, see the @axiomhq/logging library.
In the lib/axiom
folder, create a client.ts
file with the following content:
Ensure the API token you use on the client side has the appropriate permissions. Axiom recommends you create a client-side token with the only permission to ingest data into a specific dataset.
If you don’t want to expose the token to the client, use the proxy transport to send logs to Axiom.
For more information on React client side helpers, see React.
To capture traffic requests, create a middleware.ts
file in the root folder of your Next.js app with the following content:
To capture Web Vitals, add the WebVitals
component to the app/layout.tsx
file:
Send logs to Axiom from different parts of your app. Each log function call takes a message and an optional fields
object.
You can use the withAxiom
function exported from the setup file in lib/axiom/server.ts
to wrap your route handlers.
For more information on customizing the data sent to Axiom, see Advanced route handlers.
To send logs from client components, add useLogger
to your component:
To send logs from server components, use the following:
To capture errors on Next 15 or later, use the onRequestError
option. Create an instrumentation.ts
file in the src
or root folder of your Next.js app (depending on your configuration) with the following content:
Alternatively, customize the error logging by creating a custom onRequestError
function:
To capture routing errors on Next 14 or earlier, use the error handling mechanism of Next.js:
Create an error.tsx
file in the app
folder.
Inside your component function, add useLogger
to send the error to Axiom. For example:
This section describes some advanced customizations.
Instead of sending logs directly to Axiom, you can send them to a proxy endpoint in your Next.js app. This is useful if you don’t want to expose the Axiom API token to the client or if you want to send the logs from the client to transports on your server.
Create a client.ts
file in the lib/axiom
folder with the following content:
In the /app/api/axiom
folder, create a route.ts
file with the following content. This example uses /api/axiom
as the Axiom proxy path.
For more information on React client side helpers, see React.
To customize the reports sent to Axiom, use the onError
and onSuccess
functions that the createAxiomRouteHandler
function accepts in the configuration object.
In the lib/axiom/server.ts
file, use the transformRouteHandlerErrorResult
and transformRouteHandlerSuccessResult
functions to customize the data sent to Axiom by adding fields to the report object:
Changing the transformSuccessResult()
or transformErrorResult()
functions can change the shape of your data. This can affect dashboards (especially auto-generated dashboards) and other integrations.
Axiom recommends you add fields on top of the ones returned by the default transformSuccessResult()
or transformErrorResult()
functions, without replacing the default fields.
Alternatively, create your own transformSuccessResult()
or transformErrorResult()
functions:
By default, Axiom uses the following log levels:
redirect()
function are logged as info
.forbidden()
, notFound()
and unauthorized()
functions are logged as warn
.To customize this behavior, provide a custom logLevelByStatusCode()
function when logging errors from your route handler:
Internally, the status code gets captured in the transformErrorResult()
function using a getNextErrorStatusCode()
function. To compose these functions yourself, create your own getNextErrorStatusCode()
function and inject the result into the transformErrorResult()
report.
The serverContextFieldsFormatter
function adds the server execution context to the logs, this is useful to have information about the scope where the logs were generated.
By default, the createAxiomRouteHandler
function adds a request_id
field to the logs using this server context and the server context fields formatter.
The createAxiomRouteHandler
accepts a store
field in the configuration object. The store can be a map, an object, or a function that accepts a request and context. It returns a map or an object.
The fields in the store are added to the fields
object of the log report. For example, you can use this to add a trace_id
field to every log report within the same function execution in the route handler.
You can also add the server context to any function that runs in the server. For example, server actions, middleware, and server components.
This page explains how to send data from your Next.js app to Axiom.
Next.js is a popular open-source JavaScript framework built on top of React, developed by Vercel. It’s used by a wide range of companies and organizations, from startups to large enterprises, due to its performance benefits and developer-friendly features.
To send data from your Next.js app to Axiom, choose one of the following options:
The @axiomhq/nextjs library is currently in public preview. For more information, see Features states.
The choice between these options depends on your individual requirements:
The two options can collect different event types.
Event type | Axiom Vercel app | next-axiom library | @axiomhq/nextjs library |
---|---|---|---|
Application logs | Yes | Yes | Yes |
Web Vitals | No | Yes | Yes |
HTTP logs | Yes | Soon | Yes |
Build logs | Yes | No | No |
Tracing | Yes | No | Yes |
If you already use Vercel for deployments, the Axiom Vercel app can be easier to integrate into your existing experience.
The cost of these options can differ widely depending on the volume of data you transfer. The Axiom Vercel app depends on Vercel Log Drains, a feature that’s only available on paid plans. For more information, see the blog post on the changes to Vercel Log Drains.
For information on the Axiom Vercel app and migrating from the Vercel app to the next-axiom library, see Axiom Vercel app.
The rest of this page explains how to send data from your Next.js app to Axiom using the next-axiom or the @axiomhq/nextjs library.
The next-axiom library is an open-source project and welcomes your contributions. For more information, see the GitHub repository.
In your terminal, go to the root folder of your Next.js app and run the following command:
Add the following environment variables to your Next.js app:
NEXT_PUBLIC_AXIOM_DATASET
is the name of the Axiom dataset where you want to send data.NEXT_PUBLIC_AXIOM_TOKEN
is the Axiom API token you have generated.In the next.config.ts
file, wrap your Next.js configuration in withAxiom
:
To capture traffic requests, create a middleware.ts
file in the root folder of your Next.js app:
To send Web Vitals to Axiom, add the AxiomWebVitals
component from next-axiom to the app/layout.tsx
file:
Web Vitals are only sent from production deployments.
Send logs to Axiom from different parts of your app. Each log function call takes a message and an optional fields
object.
Wrap your route handlers in withAxiom
to add a logger to your request and log exceptions automatically:
To send logs from client components, add useLogger
from next-axiom to your component:
To send logs from server components, add Logger
from next-axiom to your component, and call flush before returning:
The log level defines the lowest level of logs sent to Axiom. Choose one of the following levels (from lowest to highest):
debug
is the default setting. It means that you send all logs to Axiom.info
warn
error
means that you only send the highest-level logs to Axiom.off
means that you don’t send any logs to Axiom.For example, to send all logs except for debug logs to Axiom:
To capture routing errors, use the error handling mechanism of Next.js:
app
folder.error.tsx
file.useLogger
from next-axiom to send the error to Axiom. For example:To extend the logger, use log.with
to create an intermediate logger. For example:
The @axiomhq/nextjs library is part of the Axiom JavaScript SDK, an open-source project and welcomes your contributions. For more information, see the GitHub repository.
In your terminal, go to the root folder of your Next.js app and run the following command:
Create the folder lib/axiom
to store configurations for Axiom.
Create a axiom.ts
file in the lib/axiom
folder with the following content:
In the lib/axiom
folder, create a server.ts
file with the following content:
The createAxiomRouteHandler
is a builder function that returns a wrapper for your route handlers. The wrapper handles successful responses and errors thrown within the route handler. For more information on the logger, see the @axiomhq/logging library.
In the lib/axiom
folder, create a client.ts
file with the following content:
Ensure the API token you use on the client side has the appropriate permissions. Axiom recommends you create a client-side token with the only permission to ingest data into a specific dataset.
If you don’t want to expose the token to the client, use the proxy transport to send logs to Axiom.
For more information on React client side helpers, see React.
To capture traffic requests, create a middleware.ts
file in the root folder of your Next.js app with the following content:
To capture Web Vitals, add the WebVitals
component to the app/layout.tsx
file:
Send logs to Axiom from different parts of your app. Each log function call takes a message and an optional fields
object.
You can use the withAxiom
function exported from the setup file in lib/axiom/server.ts
to wrap your route handlers.
For more information on customizing the data sent to Axiom, see Advanced route handlers.
To send logs from client components, add useLogger
to your component:
To send logs from server components, use the following:
To capture errors on Next 15 or later, use the onRequestError
option. Create an instrumentation.ts
file in the src
or root folder of your Next.js app (depending on your configuration) with the following content:
Alternatively, customize the error logging by creating a custom onRequestError
function:
To capture routing errors on Next 14 or earlier, use the error handling mechanism of Next.js:
Create an error.tsx
file in the app
folder.
Inside your component function, add useLogger
to send the error to Axiom. For example:
This section describes some advanced customizations.
Instead of sending logs directly to Axiom, you can send them to a proxy endpoint in your Next.js app. This is useful if you don’t want to expose the Axiom API token to the client or if you want to send the logs from the client to transports on your server.
Create a client.ts
file in the lib/axiom
folder with the following content:
In the /app/api/axiom
folder, create a route.ts
file with the following content. This example uses /api/axiom
as the Axiom proxy path.
For more information on React client side helpers, see React.
To customize the reports sent to Axiom, use the onError
and onSuccess
functions that the createAxiomRouteHandler
function accepts in the configuration object.
In the lib/axiom/server.ts
file, use the transformRouteHandlerErrorResult
and transformRouteHandlerSuccessResult
functions to customize the data sent to Axiom by adding fields to the report object:
Changing the transformSuccessResult()
or transformErrorResult()
functions can change the shape of your data. This can affect dashboards (especially auto-generated dashboards) and other integrations.
Axiom recommends you add fields on top of the ones returned by the default transformSuccessResult()
or transformErrorResult()
functions, without replacing the default fields.
Alternatively, create your own transformSuccessResult()
or transformErrorResult()
functions:
By default, Axiom uses the following log levels:
redirect()
function are logged as info
.forbidden()
, notFound()
and unauthorized()
functions are logged as warn
.To customize this behavior, provide a custom logLevelByStatusCode()
function when logging errors from your route handler:
Internally, the status code gets captured in the transformErrorResult()
function using a getNextErrorStatusCode()
function. To compose these functions yourself, create your own getNextErrorStatusCode()
function and inject the result into the transformErrorResult()
report.
The serverContextFieldsFormatter
function adds the server execution context to the logs, this is useful to have information about the scope where the logs were generated.
By default, the createAxiomRouteHandler
function adds a request_id
field to the logs using this server context and the server context fields formatter.
The createAxiomRouteHandler
accepts a store
field in the configuration object. The store can be a map, an object, or a function that accepts a request and context. It returns a map or an object.
The fields in the store are added to the fields
object of the log report. For example, you can use this to add a trace_id
field to every log report within the same function execution in the route handler.
You can also add the server context to any function that runs in the server. For example, server actions, middleware, and server components.