# Combine Openai Embeddings and Supabase Vector Search in One Call

> Instead of Frontend / Web firing serial fetches to OpenAI and Supabase, one pipeline hits https://api.openai.com/v1/embeddings (and sibling steps) in parallel and returns mapped JSON.

- Canonical page: https://realcomposer.com/recipes/combine-openai-and-supabase-vector-search
- Client platform: Frontend / Web
- Upstream services: OpenAI, Supabase
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/combine-openai-and-supabase-vector-search.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=combine-openai-and-supabase-vector-search&via=agent

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

Frontend / Web widgets often await OpenAI 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. **Add vault secrets for this pipeline**: Create OPENAI_API_KEY, SUPABASE_SERVICE_ROLE_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Target the real upstream URL**: Point step openai at https://api.openai.com/v1/embeddings with POST. Add output mapping fields that mirror the JSON contract your screen already expects. POST pipelines set cache TTL to 0 because RealComposer only stores GET responses at the edge.

3. **Call from Frontend / Web**: Call https://realcomposer.com/v1/workspace/combine-openai-and-supabase-vector-search with a restricted rc_live_ client token. Frontend / Web never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/combine-openai-and-supabase-vector-search', {
  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 Openai Embeddings and Supabase Vector Search in One Call",
  "method": "POST",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "openai",
      "upstreamUrlTemplate": "https://api.openai.com/v1/embeddings",
      "httpMethod": "POST",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.OPENAI_API_KEY}}",
        "Content-Type": "application/json"
      },
      "timeoutMs": 15000
    },
    {
      "stepKey": "supabase",
      "upstreamUrlTemplate": "https://{{secrets.SUPABASE_PROJECT_REF}}.supabase.co/rest/v1/rpc/match_documents",
      "httpMethod": "POST",
      "headersTemplate": {
        "apikey": "{{secrets.SUPABASE_SERVICE_ROLE_KEY}}",
        "Authorization": "Bearer {{secrets.SUPABASE_SERVICE_ROLE_KEY}}",
        "Content-Type": "application/json"
      },
      "timeoutMs": 5000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "openai.reply",
        "sourcePath": "choices.0.message.content",
        "type": "string",
        "sourceStep": "openai"
      },
      {
        "targetPath": "openai.tokens",
        "sourcePath": "usage.total_tokens",
        "type": "number",
        "sourceStep": "openai"
      },
      {
        "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-openai-and-supabase-vector-search" \
  -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-openai-and-supabase-vector-search"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "openai_reply": "sample",
  "openai_tokens": "sample",
  "supabase_rowId": "sample",
  "supabase_email": "sample"
}
```

## FAQ

### Does Frontend / Web need CORS changes after adding RealComposer?

Yes for combine-openai-and-supabase-vector-search. Ship only an rc_live_ client token in Frontend / Web; RealComposer injects OPENAI_API_KEY on https://api.openai.com/v1/embeddings.

### What cache key does RealComposer use?

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

### How do I rotate upstream credentials?

Upstream steps in combine-openai-and-supabase-vector-search honor timeoutMs and optional fallback JSON without leaking secrets.
