11# Convex Twilio Component
2+
23[ ![ npm version] ( https://badge.fury.io/js/@convex-dev%2Ftwilio.svg )] ( https://badge.fury.io/js/@convex-dev%2Ftwilio )
34
4- ** Note: Convex Components are currently in beta**
5+ ** Note: Convex Components are currently in beta.**
6+
7+ <!-- START: Include on https://convex.dev/components -->
58
69Send and receive SMS messages in your Convex app using Twilio.
710
@@ -10,7 +13,7 @@ import Twilio from "@convex-dev/twilio";
1013import { components } from " ./_generated/api" ;
1114
1215export const twilio = new Twilio (components .twilio , {
13- default_from : process .env .TWILIO_PHONE_NUMBER ! ,
16+ defaultFrom : process .env .TWILIO_PHONE_NUMBER ! ,
1417});
1518
1619export const sendSms = internalAction ({
@@ -22,6 +25,7 @@ export const sendSms = internalAction({
2225 },
2326});
2427```
28+
2529## Prerequisites
2630
2731### Twilio Phone Number
@@ -72,7 +76,7 @@ import { components } from "./_generated/api";
7276export const twilio = new Twilio (components .twilio , {
7377 // optionally pass in the default "from" phone number you'll be using
7478 // this must be a phone number you've created with Twilio
75- default_from : process .env .TWILIO_PHONE_NUMBER ! ,
79+ defaultFrom : process .env .TWILIO_PHONE_NUMBER ! ,
7680});
7781```
7882
@@ -89,30 +93,21 @@ twilio.registerRoutes(http);
8993export default http ;
9094```
9195
92- This will register two webhook HTTP handlers in your your Convex app's deployment:
93-
94- - YOUR_CONVEX_SITE_URL/twilio/message-status - capture and store delivery status of messages you ** send** .
95- - YOUR_CONVEX_SITE_URL/twilio/incoming-message - capture and store messages ** sent to** your Twilio phone number.
96-
97- Note: You may pass a custom ` http_prefix ` to ` registerRoutes ` if you want to
98- route Twilio endpoints somewhere other than ` YOUR_CONVEX_SITE_URL/twilio ` .
99-
10096## Sending Messages
10197
102- To send a message use the Convex action ` sendMessage ` exposed by the client, for example :
98+ To send a message use the Convex action ` sendMessage ` :
10399
104100``` ts
105101// convex/messages.ts
106102import { v } from " convex/values" ;
107103import { internalAction } from " ./_generated/server" ;
108104
109105export const sendSms = internalAction ({
110- args: {
111- to: v .string (),
112- body: v .string (),
113- },
114106 handler : async (ctx , args ) => {
115- return await twilio .sendMessage (ctx , args );
107+ const status = await twilio .sendMessage (ctx , {
108+ to: " +14158675309" ,
109+ body: " Hey Jenny" ,
110+ });
116111 },
117112});
118113```
@@ -122,10 +117,21 @@ By querying the message (see [below](#querying-messages)) you can check for the
122117## Receiving Messages
123118
124119To receive messages, you will associate a webhook handler provided by the component with the Twilio phone number you'd like to use.
125- The webhook handler is mounted at
126120
127- ```
128- YOUR_CONVEX_SITE_URL/twilio/incoming-message
121+ ` twilio.registerRoutes ` registers two webhook HTTP handlers in your your Convex app's deployment:
122+
123+ - ` YOUR_CONVEX_SITE_URL/twilio/message-status ` - capture and store delivery status of messages you ** send** .
124+ - ` YOUR_CONVEX_SITE_URL/twilio/incoming-message ` - capture and store messages ** sent to** your Twilio phone number.
125+
126+ Note: You may pass a custom ` httpPrefix ` to ` Twilio ` if you want to
127+ route Twilio endpoints somewhere other than ` YOUR_CONVEX_SITE_URL/twilio/* ` .
128+
129+ For instance, to route to ` YOUR_CONVEX_SITE_URL/custom-twilio/message-status ` , set:
130+
131+ ``` ts
132+ export const twilio = new Twilio (components .twilio , {
133+ httpPrefix: " /custom-twilio" ,
134+ });
129135```
130136
131137You can associate it with your Twilio phone number in two ways:
@@ -135,10 +141,6 @@ You can associate it with your Twilio phone number in two ways:
1351412 . By calling ` registerIncomingSmsHandler ` exposed by the component client, passing it the phone number's SID:
136142
137143``` ts
138- // convex/messages.ts
139-
140- // ...
141-
142144export const registerIncomingSmsHandler = internalAction ({
143145 args: {},
144146 handler : async (ctx ) => {
@@ -149,17 +151,15 @@ export const registerIncomingSmsHandler = internalAction({
149151});
150152```
151153
152- Now , incoming messages will be captured by the component and logged in the ` messages ` table.
154+ Once it is configured , incoming messages will be captured by the component and logged in the ` messages ` table.
153155
154156You can execute your own logic upon receiving an incoming message, by providing a callback when instantiating the Twilio Component client:
155157
156158``` ts
157159// convex/example.ts
158160import { Twilio , messageValidator } from " @convex-dev/twilio" ;
159161
160- const twilio = new Twilio (components .twilio , {
161- default_from: process .env .TWILIO_PHONE_NUMBER || " " ,
162- });
162+ const twilio = new Twilio (components .twilio );
163163twilio .incomingMessageCallback = internal .example .handleIncomingMessage ;
164164
165165export const handleIncomingMessage = internalMutation ({
@@ -248,75 +248,4 @@ export const getMessagesByCounterparty = query({
248248});
249249```
250250
251- # 🧑🏫 What is Convex?
252-
253- [ Convex] ( https://convex.dev ) is a hosted backend platform with a
254- built-in database that lets you write your
255- [ database schema] ( https://docs.convex.dev/database/schemas ) and
256- [ server functions] ( https://docs.convex.dev/functions ) in
257- [ TypeScript] ( https://docs.convex.dev/typescript ) . Server-side database
258- [ queries] ( https://docs.convex.dev/functions/query-functions ) automatically
259- [ cache] ( https://docs.convex.dev/functions/query-functions#caching--reactivity ) and
260- [ subscribe] ( https://docs.convex.dev/client/react#reactivity ) to data, powering a
261- [ realtime ` useQuery ` hook] ( https://docs.convex.dev/client/react#fetching-data ) in our
262- [ React client] ( https://docs.convex.dev/client/react ) . There are also clients for
263- [ Python] ( https://docs.convex.dev/client/python ) ,
264- [ Rust] ( https://docs.convex.dev/client/rust ) ,
265- [ ReactNative] ( https://docs.convex.dev/client/react-native ) , and
266- [ Node] ( https://docs.convex.dev/client/javascript ) , as well as a straightforward
267- [ HTTP API] ( https://docs.convex.dev/http-api/ ) .
268-
269- The database supports
270- [ NoSQL-style documents] ( https://docs.convex.dev/database/document-storage ) with
271- [ opt-in schema validation] ( https://docs.convex.dev/database/schemas ) ,
272- [ relationships] ( https://docs.convex.dev/database/document-ids ) and
273- [ custom indexes] ( https://docs.convex.dev/database/indexes/ )
274- (including on fields in nested objects).
275-
276- The
277- [ ` query ` ] ( https://docs.convex.dev/functions/query-functions ) and
278- [ ` mutation ` ] ( https://docs.convex.dev/functions/mutation-functions ) server functions have transactional,
279- low latency access to the database and leverage our
280- [ ` v8 ` runtime] ( https://docs.convex.dev/functions/runtimes ) with
281- [ determinism guardrails] ( https://docs.convex.dev/functions/runtimes#using-randomness-and-time-in-queries-and-mutations )
282- to provide the strongest ACID guarantees on the market:
283- immediate consistency,
284- serializable isolation, and
285- automatic conflict resolution via
286- [ optimistic multi-version concurrency control] ( https://docs.convex.dev/database/advanced/occ ) (OCC / MVCC).
287-
288- The [ ` action ` server functions] ( https://docs.convex.dev/functions/actions ) have
289- access to external APIs and enable other side-effects and non-determinism in
290- either our
291- [ optimized ` v8 ` runtime] ( https://docs.convex.dev/functions/runtimes ) or a more
292- [ flexible ` node ` runtime] ( https://docs.convex.dev/functions/runtimes#nodejs-runtime ) .
293-
294- Functions can run in the background via
295- [ scheduling] ( https://docs.convex.dev/scheduling/scheduled-functions ) and
296- [ cron jobs] ( https://docs.convex.dev/scheduling/cron-jobs ) .
297-
298- Development is cloud-first, with
299- [ hot reloads for server function] ( https://docs.convex.dev/cli#run-the-convex-dev-server ) editing via the
300- [ CLI] ( https://docs.convex.dev/cli ) ,
301- [ preview deployments] ( https://docs.convex.dev/production/hosting/preview-deployments ) ,
302- [ logging and exception reporting integrations] ( https://docs.convex.dev/production/integrations/ ) ,
303- There is a
304- [ dashboard UI] ( https://docs.convex.dev/dashboard ) to
305- [ browse and edit data] ( https://docs.convex.dev/dashboard/deployments/data ) ,
306- [ edit environment variables] ( https://docs.convex.dev/production/environment-variables ) ,
307- [ view logs] ( https://docs.convex.dev/dashboard/deployments/logs ) ,
308- [ run server functions] ( https://docs.convex.dev/dashboard/deployments/functions ) , and more.
309-
310- There are built-in features for
311- [ reactive pagination] ( https://docs.convex.dev/database/pagination ) ,
312- [ file storage] ( https://docs.convex.dev/file-storage ) ,
313- [ reactive text search] ( https://docs.convex.dev/text-search ) ,
314- [ vector search] ( https://docs.convex.dev/vector-search ) ,
315- [ https endpoints] ( https://docs.convex.dev/functions/http-actions ) (for webhooks),
316- [ snapshot import/export] ( https://docs.convex.dev/database/import-export/ ) ,
317- [ streaming import/export] ( https://docs.convex.dev/production/integrations/streaming-import-export ) , and
318- [ runtime validation] ( https://docs.convex.dev/database/schemas#validators ) for
319- [ function arguments] ( https://docs.convex.dev/functions/args-validation ) and
320- [ database data] ( https://docs.convex.dev/database/schemas#schema-validation ) .
321-
322- Everything scales automatically, and it’s [ free to start] ( https://www.convex.dev/plans ) .
251+ <!-- END: Include on https://convex.dev/components -->
0 commit comments