# Best API Gateway for React Native Mobile Apps

> React Native browsers cannot call https://jsonplaceholder.typicode.com/users/{{query.userId}} directly because of CORS and hidden credentials. RealComposer responds to OPTIONS and proxies authorized traffic.

- Canonical page: https://realcomposer.com/recipes/best-api-gateway-react-native
- Client platform: React Native
- Upstream services: REST API
- Category: Platforms & Frameworks
- JSON version: https://realcomposer.com/recipes/best-api-gateway-react-native.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=best-api-gateway-react-native&via=agent

## Problem: React Native cannot attach secret headers to REST API

React Native runs in the browser or a sandbox without a secret store. REST API rejects unsigned browser origins or requires server-only headers.

- Proxy hacks that break when vendors tighten CSP
- Zapier-only workarounds adding cost and latency
- Guest users seeing failed network requests in demos

## Solution steps

1. **Add vault secrets for this pipeline**: Create API_BEARER_TOKEN vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Configure upstream steps and output mapping**: Use GET against https://jsonplaceholder.typicode.com/users/{{query.userId}}. Allowed origins on the client token should match leave cache ttl at 0 when each response is user-specific (include userid or record id in query params before enabling cache).

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

```
const res = await fetch('https://realcomposer.com/v1/workspace/best-api-gateway-react-native', {
  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": "Best API Gateway for React Native Mobile Apps",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "upstream",
      "upstreamUrlTemplate": "https://jsonplaceholder.typicode.com/users/{{query.userId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.API_BEARER_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "userId",
        "sourcePath": "id",
        "type": "number",
        "sourceStep": "upstream"
      },
      {
        "targetPath": "name",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "upstream"
      },
      {
        "targetPath": "email",
        "sourcePath": "email",
        "type": "string",
        "sourceStep": "upstream"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/best-api-gateway-react-native" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_best-api",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "best-api-gateway-react-native"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "userId": "sample",
  "name": "sample",
  "email": "sample"
}
```

## FAQ

### What should React Native store instead of REST API secrets?

Log request IDs for best-api-gateway-react-native, not raw REST API payloads, because upstream bodies may include PII.

### How do I forward JSON bodies to POST upstreams?

Cache key for best-api-gateway-react-native: internal edge-cache path plus sorted query. Identical params hit; vary userId per user.

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

Upstream steps in best-api-gateway-react-native honor timeoutMs and optional fallback JSON without leaking secrets.
