Skip to content

Send SQL query results to Slack, e-mail and Google Sheets

Each destination has its own way in: Slack takes an incoming webhook, e-mail goes through SMTP or a mail API, and Google Sheets is written through the Sheets API with a service account. Pick the format by what the reader does next, and send only when something matches if the message is an alert. This guide covers all three, and how DataRunner handles each.

Three destinations, three ways in

DestinationHow you sendWhat you must set up
SlackPOST JSON to an incoming webhook URLA Slack app with Incoming Webhooks turned on, one webhook per channel
E-mailSMTP, or a mail provider APIA sending domain, credentials and a verified sender
Google SheetsSheets API, authenticated by a service accountA Google Cloud project with the Sheets API on, a service account key, and the spreadsheet shared with it

In all three the query part is the same: run SQL, get rows, turn rows into something the destination understands. The work is in the last step.

Slack: incoming webhooks

You create a Slack app, turn on Incoming Webhooks and add a webhook to the workspace. Slack asks which channel it may post to. That choice is fixed: a webhook is tied to one channel, and the payload cannot override it. To reach two channels, create two webhooks.

The smallest message
curl -X POST -H 'Content-type: application/json' \
  --data '{"text":"Open orders: 14"}' \
  https://hooks.slack.com/services/T000/B000/XXXX

The JSON must carry a text field, or the call fails with no_text. You can add blocks for richer layouts. The webhook URL works as a credential, so keep it out of public repositories. Slack searches for leaked webhook URLs and revokes them.

A webhook posts text. It does not upload files. If you want the CSV or Excel file itself in Slack, the app needs a bot token with the files:write scope and a channel id. Slack documents the file upload flow on its messaging pages.

E-mail: SMTP and sending reputation

Sending e-mail from a script means an SMTP server or a provider API, a sender address and a domain whose SPF and DKIM records are set. Without those, reports land in spam, and the first time a manager says "I never got it" is the day you learn this.

  • Send from a domain you control and authenticate it.
  • Keep the table short in the body, and put anything long in an attachment.
  • Use a clear subject that carries the date or period, so the inbox search works.
  • Handle bounces: an address that hard-bounces should stop being retried.

Attachment size limits come from the receiving mail server, not from your script, and they differ by provider. Check the limits of the providers your readers use before you schedule a large file.

Google Sheets: the Sheets API

A spreadsheet is the right destination when people will keep working with the rows, or when another tool reads from the sheet. You enable the Google Sheets API on a Google Cloud project, create a service account, download its JSON key, and share the spreadsheet with the service account address as Editor. Skip that last step and every call fails with 403.

Two modes cover most cases. Replace the tab on every run, so the sheet always shows the latest result. Or append rows under the existing ones, to build a log over time.

The API has per-minute quotas. At the time of writing, Google lists 300 read and 300 write requests per minute per project, and 60 of each per user per project, and it answers over-quota calls with HTTP 429. Write the rows in one request rather than one per row, and back off when you get a 429.

Table in the message, or a file attached

FormatBest forWatch out for
Inline tableA handful of rows read on a phoneLong tables wrap badly and get clipped
CSVAnother system will read it, or the volume is largeExcel guesses separators and encoding
XLSXThe reader filters, sums and pastesNeeds a library to build, and a slower open on big files
PDFA document that must look the same for allNot editable, so not for people who want to work on the numbers
Sheet tabA shared, living tableAPI quotas, and the sheet is only as private as its sharing

A rule of thumb: if the answer fits in ten rows, put it in the message. If someone will sort it, attach a file or write a sheet.

Alerts: send only when a row matches

A report goes out every time. An alert goes out only when the data says so. The difference is what keeps a Slack channel readable. Write the condition into the query, so an empty result means "all good", and skip the send when the result is empty.

Returns rows only when there is a problem
SELECT id, customer, total, due_dateFROM invoicesWHERE status = 'open'  AND due_date < current_date - 7ORDER BY due_dateLIMIT 50;

With that query on a daily schedule and an "skip when empty" rule, the channel stays quiet on good days. A row count threshold is a second option: send only when more than ten rows come back.

How DataRunner does each

DataRunner has seven delivery channels: e-mail, Slack, WhatsApp (beta), Telegram, Discord, signed webhook and Google Sheets. The formats are CSV, Excel, PDF and inline HTML.

  • Slack: you paste the incoming webhook URL. A report arrives as a message with the summary. To attach the CSV or Excel file, add a bot token with files:write and put the channel id in the target.
  • E-mail: it sends through the SMTP or Resend provider configured for the workspace, from a verified domain of yours. Bounced addresses go on a suppression list and are not retried.
  • Google Sheets: you paste the service account JSON and the spreadsheet link, share the sheet with the service account as Editor, and name the tab. Each run replaces the tab, and adding |append to the tab name appends rows under the existing ones.
  • Rules: skip the send when the result is empty, send only above a row count, or escalate to another channel past a threshold.
  • Limits: the row ceiling comes from the plan. When a run reaches it, the run stops and the report is marked truncated instead of delivering half a set silently.
  1. Add the channel under Channels and press Test. It validates the credentials against the provider.
  2. Open the report and choose the destination under Destinations.
  3. Pick the format and the delivery rule.
  4. Set the schedule and its time zone, then run it once to see the delivery in the run log.

Test confirms credentials. The run log is where you confirm that a real delivery reached each recipient and what the provider answered.

Frequently asked questions

How do I send SQL query results to Slack?
Create a Slack app with Incoming Webhooks, copy the webhook URL and POST a JSON body with a text field to it from your script. A webhook posts to one channel and cannot upload files. To attach a CSV, you need a bot token with the files:write scope.
Can I send a query result to Google Sheets automatically?
Yes. Enable the Sheets API, create a service account, share the spreadsheet with its address as Editor, and write the rows with the API on a schedule. DataRunner does this for you: you paste the key and the spreadsheet link, and each run replaces the tab or appends rows.
Should I put the result in the e-mail body or attach a file?
Put it in the body when it fits in about ten rows and people read it on a phone. Attach CSV, Excel or PDF when the reader will sort, sum or archive the data, or when the result is long.
How do I get an alert only when a query returns rows?
Write the query so it returns rows only when there is a problem, then schedule it with a rule that skips the send when the result is empty. In DataRunner this rule is part of the report delivery settings.
Can DataRunner send the result to WhatsApp?
Yes, as a beta channel. It uses Meta’s WhatsApp Cloud API with your own account, and messages outside the 24-hour window need a template approved by Meta.