# Trim Sendgrid Activity API JSON Response

> SendGrid responses include fields Frontend / Web never renders. Map id, status, and UI columns at the edge after calling https://api.sendgrid.com/v3/marketing/stats?start_date={{query.startDate}}.

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

## Problem: Unmapped SendGrid fields waste bandwidth

SendGrid returns nested CRM, audit, and configuration objects. Frontend / Web parses them on the main thread even when the UI binds three fields.

- Large arrays kept in memory after navigation
- Analytics showing poor LCP on API-heavy screens
- Developers shipping defensive null checks for unused paths

## Solution steps

1. **Bind vault keys to the Worker**: Add SENDGRID_API_KEY to RealComposer Secret Vault. Values decrypt only inside the Cloudflare Worker that calls the upstream.

2. **Target the real upstream URL**: Point step sendgrid at https://api.sendgrid.com/v3/marketing/stats?start_date={{query.startDate}} with GET. Map only the UI fields in output mapping so clients never download audit or nested blobs. 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**: Call https://realcomposer.com/v1/workspace/trim-sendgrid-activity-api with a restricted rc_live_ client token. Frontend / Web never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/trim-sendgrid-activity-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": "Trim Sendgrid Activity API JSON Response",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "sendgrid",
      "upstreamUrlTemplate": "https://api.sendgrid.com/v3/marketing/stats?start_date={{query.startDate}}",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.SENDGRID_API_KEY}}"
      },
      "timeoutMs": 3000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "delivered",
        "sourcePath": "0.stats.0.metrics.delivered",
        "type": "number",
        "sourceStep": "sendgrid"
      },
      {
        "targetPath": "opens",
        "sourcePath": "0.stats.0.metrics.opens",
        "type": "number",
        "sourceStep": "sendgrid"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/trim-sendgrid-activity-api" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_trim-sen",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "trim-sendgrid-activity-api"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "delivered": "sample",
  "opens": "sample"
}
```

## FAQ

### What should Frontend / Web store instead of SendGrid secrets?

Log request IDs for trim-sendgrid-activity-api, not raw SendGrid payloads, because upstream bodies may include PII.

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

trim-sendgrid-activity-api vault headers attach server-side, but Anthropic or OpenAI model and messages must come from the client body.

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

Rotate vault secrets for trim-sendgrid-activity-api anytime; clients keep the same rc_live_ token.
