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

# Set user token

> Set the anonymous user token for all subsequent events sent to the Algolia Insights API.

**Required ACL:** `search`

Use this method to automatically send the `userToken` with every event.
For more information, see [User token](/doc/guides/sending-events/concepts/usertoken).

## Examples

For more information about initializing the JavaScript Insights client `aa`,
see [Initialize the Insights client](/doc/libraries/search-insights/init).

```js JavaScript icon=code theme={"system"}
aa("setUserToken", "AnonymousID");
```

You can let the Insights API client generate an anonymous user token for you and store it in a cookie on the user's device.
For more information, see the `useCookie` parameter in the [`init`](/doc/libraries/search-insights/init) method.

```js JavaScript icon=code theme={"system"}
aa("init", {
  appId: "YourApplicationID",
  apiKey: "YourSearchOnlyAPIKey",
  useCookie: true,
});
```

Alternatively, you can provide your own anonymous user token on `init`.

```js JavaScript icon=code theme={"system"}
aa("init", {
  appId: "YourApplicationID",
  apiKey: "YourSearchOnlyAPIKey",
  userToken: "AnonymousID",
});
```

When the user logs in, synchronize the user token with the user ID coming from your system with [`setAuthenticatedUserToken`](/doc/libraries/search-insights/set-authenticated-user-token).

## Parameters

<ParamField body="userToken" type="string">
  Anonymous user identifier.

  See also: [User token](/doc/guides/sending-events/concepts/usertoken)
</ParamField>
