# Call Algolia Write API from Client without Master Key

> Frontend / Web cannot safely store Algolia production keys. Vault-bound headers reach https://{{secrets.ALGOLIA_APP_ID}}-dsn.algolia.net/1/indexes/{{query.index}}/query on Workers while apps call your algolia-write-api-secure-proxy edge URL.

- Canonical page: https://realcomposer.com/recipes/algolia-write-api-secure-proxy
- Client platform: Frontend / Web
- Upstream services: Algolia
- Category: Security & Secret Vault
- JSON version: https://realcomposer.com/recipes/algolia-write-api-secure-proxy.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=algolia-write-api-secure-proxy&via=agent

## Problem: Why Frontend / Web leaks Algolia credentials

Attackers routinely extract Algolia secrets from Frontend / Web builds. Direct browser calls also expose internal error payloads with hostnames.

- Production Algolia keys copied from Frontend / Web bundles
- Compliance violations after credential leaks
- Attackers replaying vault-grade scopes from a laptop

## Solution steps

1. **Bind vault keys to the Worker**: Add ALGOLIA_API_KEY to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Configure upstream steps and output mapping**: Use POST against https://{{secrets.ALGOLIA_APP_ID}}-dsn.algolia.net/1/indexes/{{query.index}}/query. Allowed origins on the client token should match post pipelines set cache ttl to 0 because realcomposer only stores get responses at the edge.

3. **Call from Frontend / Web**: Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.

```
const res = await fetch('https://realcomposer.com/v1/workspace/algolia-write-api-secure-proxy', {
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
```

## Pipeline config

Import this config into a RealComposer pipeline. `{{secrets.NAME}}` values are read from the encrypted Secret Vault at request time, so the client never sees them.

```json
{
  "name": "Call Algolia Write API from Client without Master Key",
  "method": "POST",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "algolia",
      "upstreamUrlTemplate": "https://{{secrets.ALGOLIA_APP_ID}}-dsn.algolia.net/1/indexes/{{query.index}}/query",
      "httpMethod": "POST",
      "headersTemplate": {
        "X-Algolia-API-Key": "{{secrets.ALGOLIA_API_KEY}}",
        "X-Algolia-Application-Id": "{{secrets.ALGOLIA_APP_ID}}",
        "Content-Type": "application/json"
      },
      "timeoutMs": 10000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "hitCount",
        "sourcePath": "nbHits",
        "type": "number",
        "sourceStep": "algolia"
      },
      {
        "targetPath": "firstHit",
        "sourcePath": "hits.0.objectID",
        "type": "string",
        "sourceStep": "algolia"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/algolia-write-api-secure-proxy" \
  -H "Authorization: Bearer rc_live_client_token" \
  -H "Content-Type: application/json" \
  -d '{"model":"example"}'
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_algolia-",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "algolia-write-api-secure-proxy"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "hitCount": "sample",
  "firstHit": "sample"
}
```

## FAQ

### Is it safe to log Algolia responses in Frontend / Web?

Yes for algolia-write-api-secure-proxy. Ship only an rc_live_ client token in Frontend / Web; RealComposer injects ALGOLIA_API_KEY on https://{{secrets.ALGOLIA_APP_ID}}-dsn.algolia.net/1/indexes/{{query.index}}/query.

### Will Anthropic or Linear prompt bodies be rewritten?

algolia-write-api-secure-proxy POST steps bypass caches.default even if cacheTtlSeconds is set in the dashboard.

### Can I restrict which domains call this pipeline?

Allowed origins on the algolia-write-api-secure-proxy client token restrict which domains may call the edge URL.
