# How to Filter Unnecessary Fields from REST API Response

> Answer "how to filter unnecessary fields from rest api response" by projecting REST API data server-side: upstream https://jsonplaceholder.typicode.com/users/{{query.userId}}, client sees only mapped keys.

- Canonical page: https://realcomposer.com/recipes/filter-unnecessary-fields-rest-api
- Client platform: Frontend / Web
- Upstream services: REST API
- Category: Payload Slimmer
- JSON version: https://realcomposer.com/recipes/filter-unnecessary-fields-rest-api.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=filter-unnecessary-fields-rest-api&via=agent

## Problem: REST API JSON bloat on Frontend / Web

Oversized REST API payloads inflate parse time and mobile data charges for Frontend / Web users.

- Main-thread JSON parse stalls on low-end phones
- Accidentally exposing PII fields to the client
- Downstream components re-render on unused keys

## Solution steps

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

2. **Target the real upstream URL**: 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 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/filter-unnecessary-fields-rest-api', {
  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": "How to Filter Unnecessary Fields from REST API Response",
  "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/filter-unnecessary-fields-rest-api" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_filter-u",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "filter-unnecessary-fields-rest-api"
  },
  "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

### Can Frontend / Web call REST API without holding the production key?

Point Frontend / Web at /recipes/filter-unnecessary-fields-rest-api edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins.

### Does POST traffic get cached?

filter-unnecessary-fields-rest-api keeps cache TTL at 0 because it uses POST upstream calls or per-user responses. Add ids to query strings before enabling GET cache.

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

Upstream steps in filter-unnecessary-fields-rest-api honor timeoutMs and optional fallback JSON without leaking secrets.
