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

# Step 2: Structuring a Query in Spark API

> Resolve the scope and the audience before you ask for insights

In v1, the audience, location and timeframe were written into one prompt. In v2 you resolve each part with its own endpoint first, and pass the resulting codes and IDs to [Step 3](https://api.globalwebindex.com/docs/spark-api/v2/getting-started/insight.md):

| Part of the query | Endpoint | You keep |
| :- | :- | :- |
| Datasets (optional) | `GET /v2/spark-api/datasets` | `code` of the datasets you want |
| Location and timeframe | `POST /v2/spark-api/global-filters` | `location_codes`, `wave_codes` |
| Audience | `POST /v2/spark-api/audiences/generate` or `POST /v2/spark-api/audiences/search` | `audiences[].id` |
| Combined audience (optional) | `POST /v2/spark-api/audiences/join` | `id` |
| Specific question | Sent as `prompts[]` in [Step 3](https://api.globalwebindex.com/docs/spark-api/v2/getting-started/insight.md) | — |

> [!TIP]
>
> Create an audience once and reuse its ID. Every later `POST /insights/generate` call for the same audience takes the same `enforced_audiences` value, so results stay consistent and you avoid extra calls.

## Choose datasets (optional)

Skip this section to use the default datasets for your account. To scope the analysis to specific datasets, list the datasets available to you and keep their `code` values. All codes you pass in one request must come from the same tree. See [Working with Multiple Data Sets](https://api.globalwebindex.com/docs/spark-api/v2/getting-started/working-with-multiple-data-sets.md).

```bash Request theme={null}
curl "https://api.globalwebindex.com/v2/spark-api/datasets" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

```json Response theme={null}
{
  "datasets": [
    {
      "code": "ds-core",
      "description": "Our primary data set on the attitudes and behaviors of digital consumers",
      "name": "GWI Core",
      "type": "dataset",
      "is_simulated": false,
      "children": [
        {
          "id": 446,
          "name": "GWI Syndicated",
          "type": "folder",
          "is_simulated": false,
          "children": [
            {
              "code": "ds-boost",
              "description": "Our complimentary data set covering additional product categories and brands in a selection of Core markets",
              "name": "GWI Core Plus",
              "type": "dataset",
              "is_simulated": false
            },
            {
              "code": "ds-gwi-alcohol",
              "description": "Our data set focused on consumption and purchase behaviors of alcohol consumers.",
              "name": "GWI Alcohol",
              "type": "dataset",
              "is_simulated": false
            }
          ]
        }
      ]
    },
    {
      "code": "ds-usa",
      "description": "A 360-degree view of today's America",
      "name": "GWI USA",
      "type": "dataset",
      "is_simulated": false
    }
  ]
}
```

The list is empty if your account has no dataset access. If you choose datasets, pass the same `dataset_codes` array on every later request in the chain.

## Resolve location and timeframe

Send only the locations and timeframes in `prompt`. Keep `location_codes` and `wave_codes` from the response for the next steps. The response may return default codes even when you did not name a location or timeframe; pass those on as well.

```bash Request theme={null}
curl -X POST "https://api.globalwebindex.com/v2/spark-api/global-filters" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{ "prompt": "UK in Q1 2025" }'
```

```json Response theme={null}
{
  "global_filters": {
    "location_names": [
      "UK"
    ],
    "location_codes": [
      "s2_44"
    ],
    "wave_names": [
      "Q1 2025"
    ],
    "wave_codes": [
      "q1_2025"
    ]
  }
}
```

`global_filters.inaccessible_location_names` lists any requested locations your account cannot access.

To change the scope later, see [Step 4](https://api.globalwebindex.com/docs/spark-api/v2/getting-started/follow-up.md#change-the-location-or-timeframe).

## Define the audience

### Create an audience

Describe the audience in plain language and pass the codes from the previous section. You can create up to 10 audiences in one request.

```bash Request theme={null}
curl -X POST "https://api.globalwebindex.com/v2/spark-api/audiences/generate" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "audiences": ["Gen Z"],
    "location_codes": ["s2_44"],
    "wave_codes": ["q1_2025"]
  }'
```

```json Response theme={null}
{
  "audiences": [
    {
      "id": "00000000-0000-4000-8000-000000000001",
      "name": "Generation: Gen Z",
      "description": "This audience consists of individuals who are part of Gen Z.",
      "datasets": [
        "ds-core"
      ],
      "is_simulated": false
    }
  ]
}
```

Store `audiences[].id`. Check `name` and `description` to confirm the audience matches what you asked for. A description that could not be created or matched is listed in `failed_audiences` with the reason, and the other audiences are still returned:

```json theme={null}
{
  "audiences": [
    {
      "id": "00000000-0000-4000-8000-000000000001",
      "name": "Generation: Gen Z",
      "description": "This audience consists of individuals who are part of Gen Z.",
      "datasets": ["ds-core"],
      "is_simulated": false
    }
  ],
  "failed_audiences": { "martians": "could not map prompt" }
}
```

`audiences[].datasets` shows where the audience is stored. Do not copy it into `dataset_codes`.

### Or use a saved audience

To use an audience saved on the GWI platform, search for it by name and keep the `id` of the matching result.

```bash Request theme={null}
curl -X POST "https://api.globalwebindex.com/v2/spark-api/audiences/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{ "query": "gen z", "dataset_codes": ["ds-core"] }'
```

```json Response theme={null}
{
  "audiences": [
    {
      "id": "00000000-0000-4000-8000-000000000001",
      "title": "Generation: Gen Z",
      "description": "",
      "type": "curated",
      "datasets": [
        "ds-core"
      ],
      "is_simulated": false
    },
    {
      "id": "00000000-0000-4000-8000-000000000002",
      "title": "Generation Grouping: Gen Z & Millennials ",
      "description": "",
      "type": "curated",
      "datasets": [
        "ds-core"
      ],
      "is_simulated": false
    },
    {
      "id": "00000000-0000-4000-8000-000000000003",
      "title": "OADB: UK Gen Z ",
      "description": "",
      "type": "curated",
      "datasets": [
        "ds-core"
      ],
      "is_simulated": false
    }
  ]
}
```

Optional fields: `exclude_audience_ids` leaves audiences out of the results, and `limit` sets the maximum number of results (1–100).

### Combine audiences (optional)

To analyze people who belong to two or more audiences, or to any of them, join their IDs into one audience. Pass two or more IDs from `POST /audiences/generate` or `POST /audiences/search` in `audiences`, and set `join_type`:

* `and` creates an intersection. It requires the `location_codes` and `wave_codes` from [Resolve location and timeframe](#resolve-location-and-timeframe).
* `or` creates a union.

`name` is optional. A name is generated when you omit it.

```bash Request theme={null}
curl -X POST "https://api.globalwebindex.com/v2/spark-api/audiences/join" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "audiences": [
      "00000000-0000-4000-8000-000000000001",
      "00000000-0000-4000-8000-000000000004"
    ],
    "join_type": "and",
    "location_codes": ["s2_44"],
    "wave_codes": ["q1_2025"]
  }'
```

```json Response theme={null}
{
  "id": "00000000-0000-4000-8000-000000000005",
  "name": "Gen Z AND frequent travelers",
  "description": "Intersection of Gen Z and frequent travelers.",
  "datasets": ["ds-core"],
  "is_simulated": false
}
```

Pass the returned `id` in `enforced_audiences` in [Step 3](https://api.globalwebindex.com/docs/spark-api/v2/getting-started/insight.md). Audiences that cannot be combined, for example because they share no dataset, location or wave, return [`unprocessable`](https://api.globalwebindex.com/docs/spark-api/problems/unprocessable).
