Skip to main content
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.
  • 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.

Prerequisites

Use next-axiom library

The next-axiom library is an open-source project and welcomes your contributions. For more information, see the GitHub repository.

Install next-axiom

  1. In your terminal, go to the root folder of your Next.js app and run the following command:
  2. 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.
  3. In the next.config.ts file, wrap your Next.js configuration in withAxiom:

Capture traffic requests

To capture traffic requests, create a middleware.ts file in the root folder of your Next.js app:

Web Vitals

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.

Logs

Send logs to Axiom from different parts of your app. Each log function call takes a message and an optional fields object.

Route handlers

Wrap your route handlers in withAxiom to add a logger to your request and log exceptions automatically:

Client components

To send logs from client components, add useLogger from next-axiom to your component:

Server components

To send logs from server components, add Logger from next-axiom to your component, and call flush before returning:

Log levels

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:

Capture errors

To capture routing errors, use the error handling mechanism of Next.js:
  1. Go to the app folder.
  2. Create an error.tsx file.
  3. Inside your component function, add useLogger from next-axiom to send the error to Axiom. For example:

Extend logger

To extend the logger, use log.with to create an intermediate logger. For example:

Use @axiomhq/nextjs library

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.

Install @axiomhq/nextjs

  1. In your terminal, go to the root folder of your Next.js app and run the following command:
  2. Create the folder lib/axiom to store configurations for Axiom.
  3. Create a axiom.ts file in the lib/axiom folder with the following content:
    lib/axiom/axiom.ts
  4. In the lib/axiom folder, create a server.ts file with the following content:
    lib/axiom/server.ts
    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.
  5. 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.
    lib/axiom/client.ts
For more information on React client side helpers, see React.

Capture traffic requests

To capture traffic requests, create a middleware.ts file in the root folder of your Next.js app with the following content:
middleware.ts

Web Vitals

To capture Web Vitals, add the WebVitals component to the app/layout.tsx file:
/app/layout.tsx

Logs

Send logs to Axiom from different parts of your app. Each log function call takes a message and an optional fields object.

Route handlers

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.

Client components

To send logs from client components, add useLogger to your component:

Server components

To send logs from server components, use the following:

Capture errors

Capture errors on Next 15 or later

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:
instrumentation.ts
Alternatively, customize the error logging by creating a custom onRequestError function:

Capture errors on Next 14 or earlier

To capture routing errors on Next 14 or earlier, use the error handling mechanism of Next.js:
  1. Create an error.tsx file in the app folder.
  2. Inside your component function, add useLogger to send the error to Axiom. For example:

Advanced customizations

This section describes some advanced customizations.

Proxy for client-side usage

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.
  1. Create a client.ts file in the lib/axiom folder with the following content:
    lib/axiom/client.ts
  2. 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.
    /app/api/axiom/route.ts
For more information on React client side helpers, see React.

Customize data reports sent to Axiom

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:

Change the log level from Next.js built-in function errors

By default, Axiom uses the following log levels:
  • Errors thrown by the redirect() function are logged as info.
  • Errors thrown by the 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.

Server execution context

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.

Route handlers server context

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.

Sever context on arbitrary functions

You can also add the server context to any function that runs in the server. For example, server actions, middleware, and server components.
middleware.ts