|
| 1 | +<!-- search-meta |
| 2 | +tags: [webhooks, Liveboard-schedule, REST-API, AWS-S3, Google-Drive, TypeScript, NodeJS, Express, rest-api-sdk] |
| 3 | +apis: [createWebhookConfiguration, getWebhookStorageConfig, configureCommunicationChannelPreferences, validateCommunicationChannel, LIVEBOARD_SCHEDULE, REST-API-v2] |
| 4 | +questions: |
| 5 | + - How do I receive a scheduled ThoughtSpot Liveboard through a webhook? |
| 6 | + - How do I handle ThoughtSpot webhook deliveries with PDF/CSV/XLSX attachments in Node.js? |
| 7 | + - How do I deliver Liveboard schedule files to an AWS S3 bucket with ThoughtSpot webhooks? |
| 8 | + - How do I upload ThoughtSpot scheduled Liveboard exports to Google Drive? |
| 9 | + - How do I route Liveboard schedules to the webhook communication channel? |
| 10 | +--> |
| 11 | + |
| 12 | +# Liveboard schedule webhook (Typescript) |
| 13 | + |
| 14 | +A webhook receiver, built with TypeScript and Express, for ThoughtSpot **Liveboard schedule** events. When a scheduled Liveboard runs, ThoughtSpot exports it (PDF, CSV or XLSX) and delivers it to this receiver, which uploads the files to a Google Drive folder. |
| 15 | + |
| 16 | +ThoughtSpot delivers in one of two ways, depending on whether the webhook has a storage destination: |
| 17 | + |
| 18 | +| | Direct (no storage destination) | Storage (S3 destination) | |
| 19 | +|---|---|---| |
| 20 | +| Files | Inside the request | Written to your S3 bucket by ThoughtSpot | |
| 21 | +| Request | `multipart/form-data`: a `payload` part with the event JSON, one `file` part per attachment | The event JSON plus a `files[]` list with each file's `objectKey` | |
| 22 | +| Receiver needs | Nothing extra | Its own `s3:GetObject` access to the bucket | |
| 23 | + |
| 24 | +```mermaid |
| 25 | +sequenceDiagram |
| 26 | + participant TS as ThoughtSpot |
| 27 | + participant S3 as Your S3 bucket |
| 28 | + participant Rx as This receiver |
| 29 | + participant GD as Google Drive |
| 30 | + alt Direct |
| 31 | + TS->>Rx: POST multipart (payload + files) |
| 32 | + else Storage |
| 33 | + TS->>S3: PutObject (assumes your IAM role) |
| 34 | + TS->>Rx: POST event JSON + files[] |
| 35 | + end |
| 36 | + Rx-->>TS: 200 within 5 seconds |
| 37 | + opt Storage |
| 38 | + Rx->>S3: GetObject(objectKey) |
| 39 | + end |
| 40 | + Rx->>GD: Upload files |
| 41 | +``` |
| 42 | + |
| 43 | +## Key Usage |
| 44 | + |
| 45 | +Simplified from [`src/main.ts`](src/main.ts): |
| 46 | + |
| 47 | +```typescript |
| 48 | +app.post('/webhooks/thoughtspot', checkBearerToken, express.json(), async (req, res) => { |
| 49 | + // Direct: event from the "payload" part, attachments from the "file" parts. |
| 50 | + // Storage: event is the JSON body; files[] says where each file is in S3. |
| 51 | + const { event, files } = await parseDelivery(req); |
| 52 | + |
| 53 | + // ThoughtSpot retries deliveries that fail or take longer than 5 seconds, |
| 54 | + // so skip repeats (data.msgUniqueId) and acknowledge before uploading. |
| 55 | + const key = event.data.msgUniqueId ?? event.eventId; |
| 56 | + if (!markSeen(key)) return reply(res, 200, 'Duplicate delivery; already received'); |
| 57 | + reply(res, 200, 'Webhook received successfully'); |
| 58 | + |
| 59 | + // Fetch from S3 and upload to Drive, retrying each file with backoff. |
| 60 | + queue = queue.then(() => processDelivery(event, files)); |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +File structure: |
| 65 | + |
| 66 | +``` |
| 67 | +liveboard-schedule-webhook |
| 68 | +├── src |
| 69 | +│ ├── main.ts # The receiver: parse, acknowledge, fetch from S3, upload to Drive |
| 70 | +│ ├── setup.ts # ThoughtSpot-side setup with @thoughtspot/rest-api-sdk |
| 71 | +│ └── demo.ts # npm run dev / npm test: sends sample deliveries to the receiver |
| 72 | +├── fixtures # Sample deliveries from the payload documentation |
| 73 | +├── .env.example |
| 74 | +├── .stackblitzrc |
| 75 | +└── package.json |
| 76 | +``` |
| 77 | + |
| 78 | +## Demo |
| 79 | + |
| 80 | +Open in [StackBlitz](https://stackblitz.com/github/thoughtspot/developer-examples/tree/main/rest-api/liveboard-schedule-webhook) |
| 81 | + |
| 82 | +`npm run dev` needs no ThoughtSpot instance or cloud credentials. It sends the receiver three deliveries: a direct one, a storage one (one file stored, one failed), and a retry of the first. The files are written to `out/` instead of Drive. |
| 83 | + |
| 84 | +## Documentation |
| 85 | + |
| 86 | +- [Webhooks overview](https://developers.thoughtspot.com/docs/webhooks-overview) |
| 87 | +- [Webhook for Liveboard schedule events](https://developers.thoughtspot.com/docs/webhooks-lb-schedule) |
| 88 | +- [Webhook communication channel](https://developers.thoughtspot.com/docs/webhooks-comm-channel) |
| 89 | +- [Webhook payload](https://developers.thoughtspot.com/docs/webhooks-lb-payload) |
| 90 | +- [AWS S3 storage for webhooks](https://developers.thoughtspot.com/docs/webhooks-s3-integration) |
| 91 | +- [REST API playground: create webhook](https://try-everywhere.thoughtspot.cloud/v2/#/everywhere/api/rest/playgroundV2_0?apiResourceId=http%2Fapi-endpoints%2Fwebhooks%2Fcreate-webhook-configuration) |
| 92 | + |
| 93 | +## Run locally |
| 94 | + |
| 95 | +- Clone the repository |
| 96 | + |
| 97 | +```bash |
| 98 | +git clone https://github.com/thoughtspot/developer-examples.git |
| 99 | +cd developer-examples/rest-api/liveboard-schedule-webhook |
| 100 | +``` |
| 101 | + |
| 102 | +- Install dependencies (Node 22 or later) and try the demo |
| 103 | + |
| 104 | +```bash |
| 105 | +npm install |
| 106 | +npm run dev |
| 107 | +``` |
| 108 | + |
| 109 | +- Copy `.env.example` to `.env` and set the variables: |
| 110 | + |
| 111 | +| Variable | Description | |
| 112 | +| --- | --- | |
| 113 | +| `RECEIVER_TOKEN` | Bearer token ThoughtSpot sends to the receiver; the receiver rejects requests without it. If unset, the endpoint accepts requests from anyone, so only leave it empty for local testing | |
| 114 | +| `DRIVE_FOLDER_ID` | Drive folder to upload to. It must be in a shared drive, with the service account as a member. Leave empty to write to `out/` | |
| 115 | +| `GOOGLE_APPLICATION_CREDENTIALS` | Service-account key file for Drive | |
| 116 | +| `TS_HOST`, `TS_TOKEN` | Your ThoughtSpot instance, and a bearer token for the setup calls | |
| 117 | +| `WEBHOOK_URL` | Where ThoughtSpot can reach the receiver (HTTPS). To inspect raw deliveries first, see [See a raw delivery first](#see-a-raw-delivery-first-optional) | |
| 118 | +| `S3_BUCKET`, `S3_REGION`, `S3_ROLE_ARN`, `S3_EXTERNAL_ID`, `S3_PATH_PREFIX` | Optional S3 storage destination | |
| 119 | + |
| 120 | +- Start the receiver and make it reachable from your ThoughtSpot instance, for example with [ngrok](https://ngrok.com/) or by deploying it. For storage mode, it also needs AWS credentials with `s3:GetObject` on the bucket. |
| 121 | + |
| 122 | +```bash |
| 123 | +npm start |
| 124 | +``` |
| 125 | + |
| 126 | +- Set up ThoughtSpot, in this order: |
| 127 | + |
| 128 | +| Step | Command | API | |
| 129 | +| --- | --- | --- | |
| 130 | +| 1. S3 only: get ThoughtSpot's AWS account or GCP service account and a trust-policy template | `npm run setup -- storage-config` | `GET /api/rest/2.0/webhooks/storage-config` | |
| 131 | +| 2. S3 only: create the bucket and an IAM role with that trust policy, allowed `s3:PutObject` and `s3:PutObjectAcl` | AWS console | [S3 storage docs](https://developers.thoughtspot.com/docs/webhooks-s3-integration) | |
| 132 | +| 3. Create the webhook | `npm run setup -- create-webhook` | `POST /api/rest/2.0/webhooks/create` | |
| 133 | +| 4. Send Liveboard schedules to it. Until then, they keep going to email | `npm run setup -- route-schedules` | `POST /api/rest/2.0/system/preferences/communication-channels/configure` | |
| 134 | +| 5. Send a test delivery (set `WEBHOOK_ID` to the id from step 3) | `npm run setup -- validate` | `POST /api/rest/2.0/system/communication-channels/validate` | |
| 135 | +| 6. Schedule a Liveboard | Liveboard **Schedule** menu | | |
| 136 | + |
| 137 | + For step 2, how the trust policy works depends on where your cluster is hosted: |
| 138 | + - **AWS-hosted clusters:** it uses an external ID (case-sensitive), passed as `S3_EXTERNAL_ID`. |
| 139 | + - **GCP-hosted clusters:** register `accounts.google.com` as an identity provider, and condition the trust on `accounts.google.com:sub` and `:aud`. |
| 140 | + |
| 141 | +### See a raw delivery first (optional) |
| 142 | + |
| 143 | +To see exactly what ThoughtSpot sends before you run the receiver, use a temporary [webhook.site](https://webhook.site) URL as `WEBHOOK_URL`: |
| 144 | + |
| 145 | +```bash |
| 146 | +curl -s -X POST https://webhook.site/token | jq -r '"https://webhook.site/" + .uuid' |
| 147 | +``` |
| 148 | + |
| 149 | +Run the setup steps above with that URL, then open it in a browser to see each delivery. Switch `WEBHOOK_URL` to the receiver once you're done. |
| 150 | + |
| 151 | +A free webhook.site URL has limits: |
| 152 | + |
| 153 | +- **It's public.** Anyone with the URL can read the deliveries, including the bearer token. Use test data and a throwaway `RECEIVER_TOKEN`. |
| 154 | +- **It expires after 7 days or 100 requests.** Then create a new one and update the existing webhook's URL in the Webhooks UI or with `POST /api/rest/2.0/webhooks/{webhook_identifier}/update`. You can't create a second webhook: only one is allowed per Org. |
| 155 | +- **It rejects requests over 10 MB.** ThoughtSpot records larger deliveries as failed and retries them. |
| 156 | +- **It shows only the last attachment of a direct-mode delivery**, because ThoughtSpot names every attachment part `file`. The event JSON and storage-mode deliveries arrive in full. |
| 157 | + |
| 158 | +### Before you start |
| 159 | + |
| 160 | +- Webhooks are Beta. ThoughtSpot Support enables them, together with the COMS mail agent and template-variable service they need. Without those, schedules fail with `WEBHOOK_PREREQUISITE_NOT_MET`. |
| 161 | +- Only Liveboard schedule events use the webhook channel today, and only one Liveboard schedule webhook is allowed per Org. |
| 162 | +- Minimum versions: |
| 163 | + - 10.14.0.cl: webhook APIs; |
| 164 | + - 26.3.0.cl: S3 storage; |
| 165 | + - 26.4.0.cl: `validate`; |
| 166 | + - 26.7.0.cl: `storage-config`. |
| 167 | +- Privileges: `ADMINISTRATION` or `DEVELOPER` (with RBAC, `CAN_MANAGE_WEBHOOKS` also works). Cluster-wide channel preferences need `ADMINISTRATION`. |
| 168 | + |
| 169 | +### Not covered |
| 170 | + |
| 171 | +These can be added on top of `src/main.ts`: |
| 172 | +- GCS storage destinations; |
| 173 | +- signature verification: the docs don't specify what is signed or how; |
| 174 | +- a durable queue and shared dedupe store: the in-memory ones here are lost on restart and don't work across receiver instances. |
| 175 | + |
| 176 | +## Technology labels |
| 177 | + |
| 178 | +- Typescript |
| 179 | +- NodeJS |
| 180 | +- Express |
| 181 | +- REST API SDK |
| 182 | +- AWS S3 |
| 183 | +- Google Drive |
| 184 | +- Webhook |
0 commit comments