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

# Export user profiles to an audience

> Learn how to export user profiles to an audience.

export const ApplicationID = () => <Tooltip tip="A unique alphanumeric string that identifies an Algolia application." cta="Application ID (dashboard)" href="https://dashboard.algolia.com/account/api-keys">
    application ID
  </Tooltip>;

export const APIKey = () => <Tooltip tip="An alphanumeric string that controls access to the Algolia APIs. It defines what actions are allowed, such as searching an index or adding new records." cta="API key" href="/doc/guides/security/api-keys">
    API key
  </Tooltip>;

export const Affinity = () => <Tooltip tip="Affinity is a preference score that Algolia calculates for each user based on their behavior. It reflects how likely a user is to prefer a specific facet-value pair and helps personalize their search results." cta="Affinity" href="/doc/guides/personalization/advanced-personalization/configure/prerequisites/consider-requirements-limits#affinities">
    affinity
  </Tooltip>;

This guide shows how to fetch and browse user profiles with Algolia's Advanced Personalization API.
Use these code snippets to call the API and retrieve user data for segmentation.

Exporting an audience lets you group users who share a specific <Affinity />.
Use these groups to run focused email or ad campaigns.
For example, export users who like camping or hiking.
Then send them promotional content about outdoor gear.

## Before you begin

Ensure you have a query string builder utility, such as the [`qs`](https://github.com/ljharb/qs) library available in your project.

## Fetch user profiles

The Advanced Personalization API exposes an [endpoint to retrieve user profiles](/doc/rest-api/advanced-personalization/get-users) based on specified parameters.

Here's a function to communicate with this endpoint:

```js JavaScript theme={"system"}
import qs from 'qs';

export async function fetchUserProfiles(params) {
  const { appId, apiKey, region, ...usersParams } = params;
  const response = await fetch(
    `https://ai-personalization.${region}.algolia.com/2/users${qs.stringify(
      usersParams,
      { addQueryPrefix: true }
    )}`,
    {
      headers: {
        'X-Algolia-Application-Id': appId,
        'X-Algolia-API-Key': apiKey,
      },
    }
  );

  if (!response.ok) {
    const errorMessage = await response.text();
    throw new Error(errorMessage);
  }

  const json = await response.json();

  return json;
}
```

The function takes as parameters an object containing:

* `appId`: your Algolia <ApplicationID />
* `apiKey`: your Algolia search <APIKey />, with `search`, `browse`, and `recommendation` ACLs
* `region`: the Algolia region (for example, `"us"`, `"eu"`)
* [Additional parameters for retrieving user profiles](/doc/rest-api/advanced-personalization/get-users)

## Browse user profiles

To iterate through all user profiles and handle pagination automatically, use this function:

```js JavaScript theme={"system"}
export async function browseUserProfiles(callback, params) {
  let nextPageToken = '';
  do {
    const response = await fetchUserProfiles({
      ...params,
      nextPageToken,
    });
    nextPageToken = response.nextPageToken;

    await callback(response.users);
  } while (nextPageToken);
}
```

## Process batches of user profiles

Here's an example of how to process batches of user profiles:

```js JavaScript theme={"system"}
async function processUsers(users) {
  // Perform operations with all users in the batch
}

await browseUserProfiles(processUsers, {
  appId: 'ALGOLIA_APPLICATION_ID',
  apiKey: 'ALGOLIA_SEARCH_API_KEY',
  region: 'us',
  affinity: 'category:camping',
  limit: 100
});
```

Segment users by their `category:camping` affinity and target them with focused strategies.
For example, send them an email about a discount on camping products.

Find the list of available parameters in the [REST API documentation](/doc/rest-api/advanced-personalization/get-users).
