# Call OpenAI API from WeWeb Securely

> An OpenAI key pasted into a WeWeb REST action is published with the app and readable in DevTools. Send WeWeb requests to a RealComposer pipeline instead. The edge injects OPENAI_API_KEY from the vault, checks the calling origin, and returns only the assistant message text.

- Canonical page: https://realcomposer.com/recipes/call-openai-from-weweb-securely
- Client platform: WeWeb
- Upstream services: OpenAI
- Category: Security & Secret Vault
- JSON version: https://realcomposer.com/recipes/call-openai-from-weweb-securely.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=call-openai-from-weweb-securely&via=agent

## Problem: Why WeWeb REST actions expose OpenAI keys

WeWeb builds a front-end app, so workflow actions and collection headers ship to every visitor. The WeWeb CORS proxy changes where the request is sent from, but the Authorization header is still defined in published app configuration.

- OpenAI key scraped from the published WeWeb bundle
- Unbounded token spend from bots replaying the endpoint
- Full completion payloads with usage data sent to the client

## Solution steps

1. **Store credentials in Secret Vault**: Save OPENAI_API_KEY in RealComposer so the value never enters the WeWeb project.

2. **Configure the edge pipeline**: Add the upstream steps with vault header injection, set allowed origins, and map only the fields the screen binds.

3. **Call the pipeline from a WeWeb workflow**: Call your workspace edge URL with a restricted rc_live_ client token.

```
// WeWeb workflow: REST API request action
URL:     https://realcomposer.com/v1/workspace/call-openai-from-weweb-securely
Method:  POST
Headers: Authorization: Bearer rc_live_client_token
Body:    { "messages": [{ "role": "user", "content": variables.prompt }] }
```

## 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 OpenAI API from WeWeb Securely",
  "method": "POST",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "openai",
      "upstreamUrlTemplate": "https://api.openai.com/v1/chat/completions",
      "httpMethod": "POST",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.OPENAI_API_KEY}}",
        "Content-Type": "application/json"
      },
      "timeoutMs": 15000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "reply",
        "sourceStep": "openai",
        "sourcePath": "choices.0.message.content"
      },
      {
        "targetPath": "tokens",
        "sourceStep": "openai",
        "sourcePath": "usage.total_tokens",
        "type": "number"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/call-openai-from-weweb-securely" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "id": "chatcmpl-9x",
  "object": "chat.completion",
  "model": "gpt-4o-mini",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Here is your summary."
      },
      "logprobs": null
    }
  ],
  "usage": {
    "prompt_tokens": 212,
    "completion_tokens": 48,
    "total_tokens": 260
  }
}
```

Slimmed RealComposer response:

```json
{
  "reply": "Here is your summary.",
  "tokens": 260
}
```

## FAQ

### Does the WeWeb server proxy hide my OpenAI key?

No. It helps with CORS, but headers defined in the app are still part of the published configuration. Keep the key in a vault behind your own endpoint.

### How do I stop abuse of the AI endpoint?

Set allowed origins to your WeWeb domains and apply per-client rate limits on the RealComposer pipeline.
