# Merge mailchimp campaigns with sendgrid stats

> Campaign metadata from Mailchimp plus delivery stats from SendGrid. For "merge mailchimp campaigns with sendgrid stats", store MAILCHIMP_BASIC_AUTH, SENDGRID_API_KEY in Secret Vault, call https://{{secrets.MAILCHIMP_DC}}.api.mailchimp.com/3.0/lists/{{query.listId}}/members?count=10 from a Worker, and let Frontend / Web hit only https://realcomposer.com/v1/workspace/merge-mailchimp-with-sendgrid-stats with an rc_live_ client token.

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

## Problem: Serial Mailchimp and SendGrid calls slow Frontend / Web pages

Each extra Mailchimp and SendGrid 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 MAILCHIMP_BASIC_AUTH, SENDGRID_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Set headers, mapping, and cache TTL**: Configure headersTemplate to inject vault secrets, target https://{{secrets.MAILCHIMP_DC}}.api.mailchimp.com/3.0/lists/{{query.listId}}/members?count=10, and Add output mapping fields that mirror the JSON contract your screen already expects.

3. **Call from Frontend / Web**: Call https://realcomposer.com/v1/workspace/merge-mailchimp-with-sendgrid-stats with a restricted rc_live_ client token. Frontend / Web never sends upstream secrets.

```
const res = await fetch('https://realcomposer.com/v1/workspace/merge-mailchimp-with-sendgrid-stats', {
  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": "Merge mailchimp campaigns with sendgrid stats",
  "method": "GET",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "mailchimp",
      "upstreamUrlTemplate": "https://{{secrets.MAILCHIMP_DC}}.api.mailchimp.com/3.0/lists/{{query.listId}}/members?count=10",
      "httpMethod": "GET",
      "headersTemplate": {
        "Authorization": "Basic {{secrets.MAILCHIMP_BASIC_AUTH}}"
      },
      "timeoutMs": 3000
    },
    {
      "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": "mailchimp.memberId",
        "sourcePath": "members.0.id",
        "type": "string",
        "sourceStep": "mailchimp"
      },
      {
        "targetPath": "mailchimp.email",
        "sourcePath": "members.0.email_address",
        "type": "string",
        "sourceStep": "mailchimp"
      },
      {
        "targetPath": "sendgrid.delivered",
        "sourcePath": "0.stats.0.metrics.delivered",
        "type": "number",
        "sourceStep": "sendgrid"
      },
      {
        "targetPath": "sendgrid.opens",
        "sourcePath": "0.stats.0.metrics.opens",
        "type": "number",
        "sourceStep": "sendgrid"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X GET "https://realcomposer.com/v1/workspace/merge-mailchimp-with-sendgrid-stats" \
  -H "Authorization: Bearer rc_live_client_token"
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_merge-ma",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "merge-mailchimp-with-sendgrid-stats"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "mailchimp_memberId": "sample",
  "mailchimp_email": "sample",
  "sendgrid_delivered": "sample",
  "sendgrid_opens": "sample"
}
```

## FAQ

### Does Frontend / Web need CORS changes after adding RealComposer?

Store MAILCHIMP_BASIC_AUTH, SENDGRID_API_KEY for merge-mailchimp-with-sendgrid-stats. Frontend / Web never sees decrypted values.

### When does edge caching apply to this recipe?

merge-mailchimp-with-sendgrid-stats 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?

Rotate vault secrets for merge-mailchimp-with-sendgrid-stats anytime; clients keep the same rc_live_ token.
