Tutorial: Send Slack message
This feature is in Public Preview.
In this tutorial, you create a SQL UDF operator for Lakeflow Designer that posts messages to Slack channels. SQL UDFs are the right choice when a function needs to call external APIs over HTTP. For a broader overview, see User-defined operators in Lakeflow Designer.
Overview
This operator sends messages to Slack using:
- SQL UDF: Written in SQL rather than Python.
- Unity Catalog HTTP connection: Securely manages Slack API credentials.
- Preview mode support: Prevents actual Slack API calls during workflow preview.
- Expression parameters: Allows dynamic message content from DataFrame columns.
Why SQL UDF?
For operators that need to call external APIs (like Slack, REST endpoints, webhooks), you must use SQL UDFs. Python UDFs and UDTFs cannot make HTTP requests. SQL UDFs have access to the http_request() function which works with Unity Catalog connections.
Step 1: Set up the Unity Catalog HTTP connection
Before creating the UDF, you need to set up a Unity Catalog HTTP connection to securely store your Slack API credentials. Replace <xoxb-your-slack-bot-token> with your actual Slack Bot Token. You can get this from your Slack app settings. You can use this same connection across multiple UDFs. To learn more, see Connect to external HTTP services.
-- Create a connection to store Slack credentials securely
CREATE CONNECTION my_slack_connection TYPE HTTP OPTIONS (
host 'https://slack.com',
port '443',
base_path '/api/',
bearer_token '<xoxb-your-slack-bot-token>'
);
Step 2: Create the operator YAML
Now you will create the YAML for the operator. For details about the schema, see User-defined operator YAML reference.
The YAML for this operator includes:
- Expression parameter (
msg): Allows dynamic message content from dataframe columns. - String parameter (
channel): Static channel name/ID. - Preview mode (
is_preview): A config property withformat: is_previewthat enables preview mode to prevent actual API calls during testing.
schema: user-defined-operator-v0.1.0
type: uc-udf
name: Send Slack Message
id: send_msg
version: '1.0.0'
description: Send Slack Message to a Channel
config:
type: object
properties:
msg:
type: string
format: expression
title: Message
examples:
- 'Select message column or expression'
x-ui:
widget: expression
port: input_data
channel:
type: string
title: Channel
is_preview:
type: boolean
format: is_preview
default: false
required:
- msg
- channel
additionalProperties: false
ports:
input:
- name: input_data
title: Input Data
output:
- name: output
title: Send Response Data
This includes:
Config Key | Widget | Purpose |
|---|---|---|
|
| Dynamic message content from input data. |
|
| Slack channel to send to (e.g., |
| n/a | A boolean config property with |
Step 3: Create the Unity Catalog function
When creating SQL UDFs, there are a few things that are uncommon compared to most SQL queries:
- Use the
RETURNsyntax instead ofAS $$. - Embed the YAML configuration in a SQL comment block (
/* ... */). - Can use the
http_requestfunction for API calls.
CREATE OR REPLACE FUNCTION main.my_schema.send_slack_msg(
msg STRING,
channel STRING,
is_preview BOOLEAN
)
RETURNS STRING
RETURN (/*
schema: user-defined-operator-v0.1.0
type: uc-udf
name: Send Slack Message
id: send_msg
version: "1.0.0"
description: Send Slack Message to a Channel
config:
type: object
properties:
msg:
type: string
format: expression
title: Message
examples:
- "Select message column or expression"
x-ui:
widget: expression
port: input_data
channel:
type: string
title: Channel
is_preview:
type: boolean
format: is_preview
default: false
required:
- msg
- channel
additionalProperties: false
ports:
input:
- name: input_data
title: Input Data
output:
- name: output
title: Send Response Data
*/
CASE
WHEN NOT is_preview THEN
http_request(
conn => 'my_slack_connection',
method => 'POST',
path => 'chat.postMessage',
json => to_json(named_struct('channel', channel, 'text', msg)),
headers => map('Content-Type', 'application/json;charset=utf-8')
).text
ELSE 'Preview mode - no message sent to ' || channel
END
);
This SQL function includes the following features:
Feature | Purpose |
|---|---|
| Makes HTTP calls to external APIs. |
| References the UC connection for authentication. |
| Constructs the JSON payload for the Slack API. |
YAML comment block | Used by Lakeflow Designer to create the operator. |
| Implements preview mode logic. |
Step 4: Test the function
Next, test the function to make sure it works before registering it as an operator.
Test in preview mode first, to avoid sending a Slack message:
-- Test in preview mode (won't send real message)
SELECT main.my_schema.send_slack_msg(
'Hello from Lakeflow Designer!',
'#test-channel',
true -- is_preview = true
) AS result;
-- Expected result: "Preview mode - no message sent to #test-channel"
Test with external API call (will send a message to Slack):
-- Test with real API call (USE WITH CAUTION!)
SELECT main.my_schema.send_slack_msg(
'Hello from Lakeflow Designer!',
'#test-channel',
false -- is_preview = false
) AS result;
-- Expected: Slack API response JSON
Step 5: Register the operator
Add the operator to your .user_defined_operators.yaml file:
operators:
- catalog: main
schema: my_schema
functionName: send_slack_msg
If you define this file in your user folder, it only appears for you. For more information, see Make your operator discoverable.
Step 6: Set up permissions
For SQL UDFs that use Unity Catalog connections, users need an additional permission:
-- Schema and function access
GRANT USE SCHEMA ON SCHEMA main.my_schema TO `<user>`;
GRANT EXECUTE ON FUNCTION main.my_schema.send_slack_msg TO `<user>`;
-- Connection access (required for API calls)
GRANT USE CONNECTION ON CONNECTION my_slack_connection TO `<user>`;
Without the USE CONNECTION permission, users won't be able to make API calls even if they can execute the function.
Using the operator in Lakeflow Designer
After it's registered, the operator will appear in Lakeflow Designer with:
- An input port to connect your data source.
- An expression picker to select which column contains the message content.
- A text input for the Slack channel.
Users can send notifications based on their data — for example, alerting when certain thresholds are exceeded.
Common use cases
- Alerts — Send notifications when data quality issues are detected.
- Notifications — Notify teams when workflows complete.
- Webhooks — Call external APIs to trigger downstream processes.
- Logging — Send audit messages to external systems.
Best practices for building API-calling operators
- Always use preview mode — Add an
is_previewconfig property withformat: is_previewto prevent accidental API calls. - Use Unity Catalog connections — Never hardcode credentials in your UDF. Unity Catalog connections are only available in SQL UDFs.
- Handle errors gracefully — API calls can fail; consider what to return on error.
- Test thoroughly — Use preview mode during development.
- Document the connection setup — Users need to know what connection to create.