# Combine anthropic with supabase vector rag search

> Anthropic completion plus Supabase vector match results composed at edge. For "combine anthropic with supabase vector rag search", store ANTHROPIC_API_KEY, SUPABASE_SERVICE_ROLE_KEY in Secret Vault, call https://api.anthropic.com/v1/messages from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/combine-anthropic-supabase-vector-rag with an rc_live_ client token. POST upstream calls are not stored in caches.default; clients must send JSON bodies and headers forward unchanged.

- Canonical page: https://realcomposer.com/recipes/combine-anthropic-supabase-vector-rag
- Client platform: Next.js / React
- Upstream services: Anthropic, Supabase
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/combine-anthropic-supabase-vector-rag.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=combine-anthropic-supabase-vector-rag&via=agent

## Problem: Waterfall latency from multiple Anthropic and Supabase requests

Next.js / React widgets often await Anthropic and Supabase sequentially, multiplying TLS handshakes on cellular networks and amplifying failure rates.

- Race conditions between parallel manual fetches
- Higher mobile data usage from redundant JSON
- Timeouts when any upstream exceeds cellular RTT

## Solution steps

1. **Store upstream credentials in Secret Vault**: Add ANTHROPIC_API_KEY, SUPABASE_SERVICE_ROLE_KEY to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Configure upstream steps and output mapping**: Configure headersTemplate to inject vault secrets, target https://api.anthropic.com/v1/messages, and Add output mapping fields that mirror the JSON contract your screen already expects.

3. **Call from Next.js / React**: Allowed origins on the token should list every Next.js / React host that calls this URL.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/combine-anthropic-supabase-vector-rag', {
  cache: 'no-store',
  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": "Combine anthropic with supabase vector rag search",
  "method": "POST",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "anthropic",
      "upstreamUrlTemplate": "https://api.anthropic.com/v1/messages",
      "httpMethod": "POST",
      "headersTemplate": {
        "x-api-key": "{{secrets.ANTHROPIC_API_KEY}}",
        "anthropic-version": "2023-06-01",
        "Content-Type": "application/json"
      },
      "timeoutMs": 10000
    },
    {
      "stepKey": "supabase",
      "upstreamUrlTemplate": "https://{{secrets.SUPABASE_PROJECT_REF}}.supabase.co/rest/v1/{{query.table}}?{{query.filter}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "apikey": "{{secrets.SUPABASE_SERVICE_ROLE_KEY}}",
        "Authorization": "Bearer {{secrets.SUPABASE_SERVICE_ROLE_KEY}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "anthropic.role",
        "sourcePath": "content.0.text",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "anthropic.model",
        "sourcePath": "model",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "supabase.rowId",
        "sourcePath": "0.id",
        "type": "string",
        "sourceStep": "supabase"
      },
      {
        "targetPath": "supabase.email",
        "sourcePath": "0.email",
        "type": "string",
        "sourceStep": "supabase"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/combine-anthropic-supabase-vector-rag" \
  -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_combine-",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "combine-anthropic-supabase-vector-rag"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "anthropic_role": "sample",
  "anthropic_model": "sample",
  "supabase_rowId": "sample",
  "supabase_email": "sample"
}
```

## FAQ

### Does Next.js / React need CORS changes after adding RealComposer?

Yes for combine-anthropic-supabase-vector-rag. Ship only an rc_live_ client token in Next.js / React; RealComposer injects ANTHROPIC_API_KEY on https://api.anthropic.com/v1/messages.

### What cache key does RealComposer use?

combine-anthropic-supabase-vector-rag forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

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

Allowed origins on the combine-anthropic-supabase-vector-rag client token restrict which domains may call the edge URL.
