How to send SMS messages from Supabase
Sending SMS messages to your users can be a key communication feature in your applications. In this video we will show you how to send an SMS message from Supabase.
In the video we take a look at connecting to the Sinch API for sending sms messages to our users from Supabase.
Using the http extension to Supabase, we work through the http request format, the API keys and endpoint requirements and make a request to the API for sending the sms.
We also look at the Flutterflow side and see how we can call the request from our applications, passing the parameters we need to select the users information from our database tables.
Here is the video. All of the code you see on screen can be downloaded below.
Custom code to send SMS messages from Supabase
This is the main function for constructing the message, connecting to the API and sending the SMS messages. Remember to change the variable names and add the API endpoint for the SMS provider you are using.
CREATE OR REPLACE FUNCTION send_sms(currentuser uuid) RETURNS INTEGER SECURITY DEFINER AS $$
DECLARE
request_data extensions.http_request;
response_data extensions.http_response;
response_code INTEGER;
SINCH_API_KEY text;
phone_number text;
pin_code text;
json_content jsonb;
BEGIN
SELECT value::text INTO SINCH_API_KEY FROM private.keys WHERE key = 'SINCH_API_KEY';
SELECT phone INTO phone_number FROM public.users WHERE userid = currentuser;
SELECT code INTO pin_code FROM public.verify WHERE userid = currentuser;
-- Construct JSON using jsonb_build_object
json_content := jsonb_build_object(
'from', '447520651678',
'to', ARRAY[phone_number],
'body', CASE WHEN pin_code IS NOT NULL THEN 'Your six-digit code is ' || pin_code ELSE 'No code generated' END
);
-- Set the values for the http_request record
request_data.method := 'POST';
request_data.uri := 'https://THE_API_ENDPOINT';
request_data.headers := ARRAY[
('Authorization', 'Bearer ' || SINCH_API_KEY)
];
request_data.content_type := 'application/json';
request_data.content := json_content::text;
-- Making the HTTP POST request
response_data := http(request_data)::extensions.http_response;
-- Parsing the response
response_code := response_data.status;
-- Return the response code
RETURN response_code;
END;
$$ LANGUAGE plpgsql;
This is the function for creating the private schema and the private.keys table within your Subabase project. You can add the API keys to the table directly once the table is created.
CREATE SCHEMA IF NOT EXISTS private;
CREATE TABLE IF NOT EXISTS private.keys (
key text primary key not null,
value text
);
REVOKE ALL ON TABLE private.keys FROM PUBLIC;
To connect to your API from the SQL editor, run this command. Remember to change the UUID of the user you are sending the SMS to.
SELECT send_sms('UUID')
How to send SMS messages from Flutterflow
For your custom action in Flutterflow use this code and pass in as the parameter the UUID of the user you are sending the SMS message to. This will call the send-sms SQL function we have above.
Future<void> sendSms(
String puserid,
) async {
try {
final supabase = SupaFlow.client;
await supabase.rpc('send_sms', params: {
'currentuser': puserid,
});
} catch (e) {
print('Exception:$e');
}
}