> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apitally.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Setup guide for Elysia

> Just a few simple steps to get started with Apitally.

export const language_0 = "javascript"

export const framework_0 = "Elysia"

This page guides you through the steps of configuring your [Elysia](https://elysiajs.com/) application to work with Apitally. If you don't have an account yet, now would be a good time to [sign up](https://app.apitally.io/?signup).

Once you're done with this guide, you will be able to:

* Get detailed metrics on API usage, errors, and performance
* Track API adoption and usage by individual consumers
* Log individual API requests, responses, and correlated application logs
* See what's causing slow API requests with traces
* Monitor uptime and set up custom alerts

## Create app

To get started, create a new app in the [Apitally dashboard](https://app.apitally.io/apps) and select {framework_0} as your framework.

<img src="https://assets.apitally.io/docs/2025-12-08/create-app.webp" alt="Create app" className="rounded-xl" />

You can also configure the environments (e.g. `prod` and `dev`) for your app, or simply accept the defaults.

After submitting, you will see tailored setup instructions for your app. These include code snippets you can copy and paste into your project.

<Note>
  The **client ID** provided in the setup instructions uniquely identifies your app for the purpose of data ingestion only. It does not grant any kind of read access to your data.
</Note>

## Add middleware

Next, install the [Apitally SDK](/sdk-reference/javascript/overview) in your Elysia project.

```shell theme={null}
bun add apitally
```

Add the Apitally plugin to your Elysia application and provide the `clientId` for your app.
You'll find the `clientId` on the *Setup instructions* page for your app in the Apitally dashboard, which is displayed immediately after creating the app.

```javascript theme={null}
import { Elysia } from "elysia";
import { apitallyPlugin } from "apitally/elysia";

const app = new Elysia()
  .use(
    apitallyPlugin({
      clientId: "your-client-id",
      env: "dev", // or "prod" etc.
    }),
  )
  .get("/", () => "hello");
```

Deploy your application with these changes, or restart if you're testing locally.

<Check>
  At this point the basic setup for your application is complete and you will start seeing data in the Apitally dashboard.
</Check>

## Identify consumers

To analyze and filter API traffic by consumers, you can associate requests with consumer identifiers in your application.

In most cases, use the authenticated identity to identify the consumer.
The identifier should be a string, such as a username, email address, or any other unique identifier.

Optionally, you can also provide a display name and group for each consumer.

Set the `apitally.consumer` property to associate requests with consumers, for example in a `derive` lifecycle function.

<CodeGroup>
  ```javascript Identifier only theme={null}
  app.derive(async ({ apitally, jwt, cookie: { auth } }) => {
    const profile = await jwt.verify(auth);
    apitally.consumer = profile.name;
  });
  ```

  ```javascript With name and group theme={null}
  app.derive(async ({ apitally, jwt, cookie: { auth } }) => {
    const profile = await jwt.verify(auth);
    apitally.consumer = {
      identifier: profile.id,
      name: profile.name,
      group: profile.role,
    };
  });
  ```
</CodeGroup>

<Check>
  The *Consumers* dashboard now shows all consumers that have made requests to your application. You can also filter other dashboards by consumer.
</Check>

## Configure request logging

Logging of individual requests and responses is disabled by default to protect potentially sensitive data.
If you enable it, you can configure in detail what parts of the request and response should be logged.
You can also mask sensitive information (e.g. in headers) and exclude certain requests from logging.

The SDK applies [default masking rules](/data-privacy#data-masking) for common sensitive headers, query parameters and request/response body fields.

{language_0 == "python" && <p>Check out the <a href="/sdk-reference/python/configuration">SDK reference</a> to learn more about the request logging configuration options.</p>}

{language_0 == "javascript" && <p>Check out the <a href="/sdk-reference/javascript/configuration">SDK reference</a> to learn more about the request logging configuration options.</p>}

{language_0 == "go" && <p>Check out the <a href="/sdk-reference/go/configuration">SDK reference</a> to learn more about the request logging configuration options.</p>}

<CodeGroup>
  ```javascript Basic example theme={null}
  import { Elysia } from "elysia";
  import { apitallyPlugin } from "apitally/elysia";

  const app = new Elysia()
    .use(
      apitallyPlugin({
        clientId: "your-client-id",
        env: "dev", // or "prod" etc.
        requestLogging: {
          enabled: true,
          logRequestHeaders: true,
          logRequestBody: true,
          logResponseBody: true,
          captureLogs: true,
          captureTraces: false, // requires instrumentation
        },
      }),
    )
    .get("/", () => "hello");
  ```

  ```javascript Advanced example theme={null}
  import { Elysia } from "elysia";
  import { apitallyPlugin } from "apitally/elysia";

  const app = new Elysia()
    .use(
      apitallyPlugin({
        clientId: "your-client-id",
        env: "dev", // or "prod" etc.
        requestLogging: {
          enabled: true,
          logQueryParams: true,
          logRequestHeaders: true,
          logRequestBody: true,
          logResponseHeaders: true,
          logResponseBody: true,
          logException: true,
          captureLogs: true,
          captureTraces: false, // requires instrumentation
          // Mask query parameters using regex
          maskQueryParams: [/^card_number$/i],
          // Mask headers using regex
          maskHeaders: [/^X-Sensitive-Header$/i],
          // Mask request/response body fields using regex
          maskBodyFields: [/^sensitive_field$/i],
          // Mask request/response body with custom logic via callbacks (e.g. for admin endpoints)
          // The callback should return null to mask the whole body, or return the (modified) raw body
          maskRequestBodyCallback: (request) => request.path?.startsWith("/admin/") ? null : request.body,
          maskResponseBodyCallback: (request, response) => request.path?.startsWith("/admin/") ? null : response.body,
          // Exclude paths from request logging using regex (common health check paths are excluded by default)
          excludePaths: [/\/metrics$/],
          // Exclude requests from logging with custom logic via callback (e.g. exclude requests from a specific consumer)
          // The callback should return true to exclude the request
          excludeCallback: (request, response) => request.consumer === "some-consumer",
        },
      }),
    )
    .get("/", () => "hello");
  ```
</CodeGroup>

<Check>
  The *Request logs* dashboard now shows individual requests handled by your application. You can filter, search, and inspect them in detail.
</Check>

## Enable tracing

Tracing gives you a detailed breakdown of time spent during the handling of each request, showing the duration of database queries, HTTP calls, and other operations.

To enable tracing, set `captureTraces` to `true` and instrument the libraries you want to trace with OpenTelemetry. See the [tracing guide](/sdk-reference/javascript/tracing) for further instructions.
