# Pass the user agent to Hypertune

## Overview

You may need to pass the user agent to Hypertune, e.g. to include it in analytics event payloads, target features or experiments, or provide personalization context to AI loops.

A common pattern is detecting crawlers or bots and returning a specific feature variant early. This can ensure they don't enter an experiment, keeping exposures and analytics clean.

## Setup

To pass the user agent to Hypertune, first add a `userAgent` field to your `User` input type:

```graphql
input User {
  id: String!
  name: String!
  email: String!
  userAgent: String!
}
```

Then get the user agent from the `user-agent` header and pass it to Hypertune:

{% code title="lib/getHypertune.ts" %}

```typescript
import "server-only";
import { unstable_noStore as noStore } from "next/cache";
import { headers } from "next/headers";
import { createSource } from "@/generated/hypertune";

const hypertuneSource = createSource({
  token: process.env.NEXT_PUBLIC_HYPERTUNE_TOKEN!,
});

export default async function getHypertune() {
  noStore();
  await hypertuneSource.initIfNeeded(); // Check for flag updates

  const headersList = await headers();
  const userAgent = headersList.get("user-agent") ?? "";

  return hypertuneSource.root({
    args: {
      context: {
        environment: process.env.NODE_ENV,
        user: { id: "1", name: "Test", email: "hi@test.com", userAgent },
      },
    },
  });
}
```

{% endcode %}

Now you can use the user agent in:

* Analytics event payloads
* Flag targeting rules


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hypertune.com/guides/pass-the-user-agent-to-hypertune.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
