
# Export API v3.0.0

Universal export and replay API with two clean endpoints

## Endpoints

### POST /export/data
Export data from D1 or BigQuery to Google Sheets or CSV

**Request (D1 to Google Sheets):**
```json
{
  "dataSource": "d1",
  "database": "apas_workflows",
  "query": "SELECT * FROM workflow_executions WHERE status='failed' LIMIT 100",
  "sheetName": "Failed Workflows - October 2025"
}
```

**Request (BigQuery to Google Sheets):**
```json
{
  "dataSource": "bigquery",
  "client": "phallofill",
  "query": "SELECT * FROM \`apas-phallofill.phallofill_apas.form_submissions\` LIMIT 100",
  "sheetName": "Phallofill Forms"
}
```

**Request (CSV export):**
```json
{
  "dataSource": "d1",
  "database": "apas_workflows",
  "query": "SELECT * FROM workflow_executions LIMIT 100",
  "format": "csv"
}
```

**Response (Sheets):**
```json
{
  "success": true,
  "dataSource": "d1",
  "sheetUrl": "https://docs.google.com/spreadsheets/d/...",
  "sheetId": "...",
  "rowCount": 47,
  "timestamp": "2025-10-05T12:00:00.000Z"
}
```

**Response (CSV):** CSV file download

### POST /export/replay
Replay data from D1, BigQuery, or test to workflow endpoints with automatic routing

**Request (D1 replay):**
```json
{
  "dataSource": "d1",
  "database": "apas_forms",
  "query": "SELECT * FROM apas_submissions WHERE webhook_sent=0 AND client_id='phallofill'",
  "client": "phallofill",
  "workflowType": "form-submission",
  "exportResults": true
}
```

**Request (BigQuery replay):**
```json
{
  "dataSource": "bigquery",
  "client": "phallofill",
  "query": "SELECT * FROM \`apas-phallofill.phallofill_apas.google_ads_conversions\` WHERE upload_status = 'failed'",
  "workflowType": "google-ads-conversion",
  "exportResults": true
}
```

**Request (Test data - safe testing):**
```json
{
  "dataSource": "test",
  "database": "apas_forms",
  "query": "SELECT 'John' AS firstname, 'Doe' AS lastname, 'john@test.com' AS email",
  "client": "phallofill",
  "workflowType": "form-submission",
  "exportResults": false
}
```

**Response:**
```json
{
  "success": true,
  "replay": {
    "total": 15,
    "success": 14,
    "failed": 1,
    "errors": [...],
    "startTime": "2025-10-05T12:00:00.000Z",
    "endTime": "2025-10-05T12:00:15.456Z",
    "duration": 15456
  },
  "sheetUrl": "https://docs.google.com/spreadsheets/d/..." (if exportResults=true),
  "timestamp": "2025-10-05T12:00:15.456Z"
}
```

**Workflow Types (automatic routing):**
- `form-submission` → https://api.apas.cloud/webhook/apas/forms
- `call` → https://api.apas.cloud/webhook/apas/calls
- `offline-conversion` → https://api.apas.cloud/webhook/{client}/offline-conversions
- `google-ads-conversion` → https://api.apas.cloud/webhook/apas/google-ads
- `bing-ads-conversion` → https://api.apas.cloud/webhook/apas/bing-ads
- `meta-conversion` → https://api.apas.cloud/webhook/apas/meta

### GET /health
Health check endpoint

**Response:**
```json
{
  "status": "healthy",
  "timestamp": "2025-10-05T12:00:00.000Z",
  "version": "3.0.0"
}
```

## Data Sources
- `d1`: Cloudflare D1 databases (14 databases)
- `bigquery`: Google BigQuery (via bigquery-api.apascloud.com)
- `test`: Test data with hardcoded SQL values (replay only)

## Available D1 Databases
apas_workflows, apas_queue, apas_forms, apas_calls, apas-cloud-majestic-hits,
google-ads-conversions, bigquery-tracking, bing-ads-conversions, meta_conversions,
apas_offline_conversions, apas_ads, apas_intelligence, dataform-tracking, n8n-database
        