# How to reduce firebase firestore json payload size

> Strips document names revision metadata and map noise at edge. For "how to reduce firebase firestore json payload size", store FIREBASE_ACCESS_TOKEN in Secret Vault, call https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}} from a Worker, and let Flutter / React Native / Swift hit only https://realcomposer.com/v1/workspace/reduce-firebase-firestore-json-size with an rc_live_ client token.

- Canonical page: https://realcomposer.com/recipes/reduce-firebase-firestore-json-size
- Client platform: Flutter / React Native / Swift
- Upstream services: Firebase
- Category: Payload Slimmer
- JSON version: https://realcomposer.com/recipes/reduce-firebase-firestore-json-size.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=reduce-firebase-firestore-json-size&via=agent

## Problem: Firebase JSON bloat on Flutter / React Native / Swift

Oversized Firebase payloads inflate parse time and mobile data charges for Flutter / React Native / Swift 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. **Add vault secrets for this pipeline**: Add FIREBASE_ACCESS_TOKEN to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Set headers, mapping, and cache TTL**: Use GET against https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}}. 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 Flutter / React Native / Swift**: Call https://realcomposer.com/v1/workspace/reduce-firebase-firestore-json-size with a restricted rc_live_ client token. Flutter / React Native / Swift never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/reduce-firebase-firestore-json-size', {
  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 reduce firebase firestore json payload size",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "firebase",
      "upstreamUrlTemplate": "https://firestore.googleapis.com/v1/projects/{{query.projectId}}/databases/(default)/documents/{{query.collection}}/{{query.docId}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.FIREBASE_ACCESS_TOKEN}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "docName",
        "sourcePath": "name",
        "type": "string",
        "sourceStep": "firebase"
      },
      {
        "targetPath": "fields",
        "sourcePath": "fields",
        "type": "object",
        "sourceStep": "firebase"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/reduce-firebase-firestore-json-size" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_reduce-f",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "reduce-firebase-firestore-json-size"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "docName": "sample",
  "fields": "sample"
}
```

## FAQ

### Can Flutter / React Native / Swift call Firebase without holding the production key?

Point Flutter / React Native / Swift at /recipes/reduce-firebase-firestore-json-size edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins.

### Does POST traffic get cached?

reduce-firebase-firestore-json-size keeps cache TTL at 0 because it uses POST upstream calls or per-user responses. Add ids to query strings before enabling GET cache.

### What happens if an upstream step times out?

Rotate vault secrets for reduce-firebase-firestore-json-size anytime; clients keep the same rc_live_ token.
