# Call Two REST Apis in Parallel and Return Merged Payload

> To call two rest apis in parallel and return merged payload, configure parallel steps for REST API. Frontend / Web performs one authorized GET/POST to RealComposer and receives a merged payload.

- Canonical page: https://realcomposer.com/recipes/parallel-api-fetch-merge-payload
- Client platform: Frontend / Web
- Upstream services: REST API
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/parallel-api-fetch-merge-payload.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=parallel-api-fetch-merge-payload&via=agent

## Problem: Serial REST API calls slow Frontend / Web pages

Each extra REST API hop adds hundreds of milliseconds and another OAuth refresh path your UI must manage.

- One slow upstream blocking an entire page
- Partial failures leaving UI in inconsistent states
- Duplicate auth refresh logic per service

## 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. **Target the real upstream URL**: Use GET against https://jsonplaceholder.typicode.com/posts/1. 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 Frontend / Web**: Allowed origins on the token should list every Frontend / Web host that calls this URL.

```
const res = await fetch('https://realcomposer.com/v1/workspace/parallel-api-fetch-merge-payload', {
  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 Two REST Apis in Parallel and Return Merged Payload",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "posts",
      "upstreamUrlTemplate": "https://jsonplaceholder.typicode.com/posts/1",
      "httpMethod": "GET",
      "headersTemplate": {},
      "timeoutMs": 3000
    },
    {
      "stepKey": "user",
      "upstreamUrlTemplate": "https://jsonplaceholder.typicode.com/users/1",
      "httpMethod": "GET",
      "headersTemplate": {},
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "posts.userId",
        "sourcePath": "id",
        "type": "number",
        "sourceStep": "posts"
      },
      {
        "targetPath": "posts.name",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "posts"
      },
      {
        "targetPath": "user.userId",
        "sourcePath": "id",
        "type": "number",
        "sourceStep": "user"
      },
      {
        "targetPath": "user.name",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "user"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/parallel-api-fetch-merge-payload" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_parallel",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "parallel-api-fetch-merge-payload"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "posts_userId": "sample",
  "posts_name": "sample",
  "user_userId": "sample",
  "user_name": "sample"
}
```

## FAQ

### What should Frontend / Web store instead of REST API secrets?

Yes for parallel-api-fetch-merge-payload. Ship only an rc_live_ client token in Frontend / Web; RealComposer injects API_BEARER_TOKEN on https://jsonplaceholder.typicode.com/posts/1.

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

parallel-api-fetch-merge-payload forwards client JSON to POST upstreams unchanged; secrets interpolate only in URLs and header templates.

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

Rotate vault secrets for parallel-api-fetch-merge-payload anytime; clients keep the same rc_live_ token.
