# Workflow: Search, Count, Aggregate, and Export Store Records

Use this workflow when a user asks questions about an existing store.

## Before live calls

- If no NiftyImages API key is available, ask the user for one before calling `/v2`.
- Do not invent, guess, log, or expose the API key.
- If the user only wants planning or code generation, continue without an API key.
- Account API keys can access all stores for the account. Store API Keys start with `store-` and can access only one store.
- If no `storeId` is available, call `GET /v2/stores/access` first. If the key is account-scoped, list or search stores; if it is store-scoped, use the returned `storeId`.

## Find the store

Use this when the key is account-scoped:

```bash
curl "https://dev.niftyimages.com/v2/stores?q=loyalty&limit=10" \
  -H "Authorization: Bearer $NIFTYIMAGES_API_KEY"
```

Then keep the returned `storeId`.

Use this when key scope is unknown or a Store API Key may be in use:

```bash
curl https://dev.niftyimages.com/v2/stores/access \
  -H "Authorization: Bearer $NIFTYIMAGES_API_KEY"
```

## Count records

```bash
curl https://dev.niftyimages.com/v2/stores/{storeId}/records/count \
  -H "Authorization: Bearer $NIFTYIMAGES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "filters": [
      { "field": "state", "op": "eq", "value": "CA" }
    ]
  }'
```

## Search records

```bash
curl https://dev.niftyimages.com/v2/stores/{storeId}/records/search \
  -H "Authorization: Bearer $NIFTYIMAGES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "filters": [
      { "field": "status", "op": "eq", "value": "VIP" }
    ],
    "match": "all",
    "limit": 25
  }'
```

## Aggregate numeric records

```bash
curl https://dev.niftyimages.com/v2/stores/{storeId}/records/aggregate \
  -H "Authorization: Bearer $NIFTYIMAGES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "field": "points",
    "op": "sum",
    "filters": [
      { "field": "status", "op": "eq", "value": "VIP" }
    ]
  }'
```

## Export records

```bash
curl "https://dev.niftyimages.com/v2/stores/{storeId}/records/export?format=csv&limit=1000" \
  -H "Authorization: Bearer $NIFTYIMAGES_API_KEY"
```

## Agent notes

- Search is capped at 100 hits and has no cursor in the first version.
- Use `count` before search when the user asks "how many".
- Use `aggregate` only on number-typed properties.
- Use `export` for bulk handoff, not for narrow conversational summaries.
