SQL Server Requires Chilkat v11.0.0+
SQL Server
Send an SMS verification code with Twilio Verify
See more Twilio Examples
Live SMS. When a user requests a code, pass their phone number in E.164 format (for example, +12025550123) and select the SMS channel. Create a Verify Service in the Twilio Console first. Supply its VA service SID and an API key with access to that service. For a live test, use a phone you control. Messaging and verification charges may apply.
Twilio generates the code and sends it to the phone. A pending response means the application can prompt for the code. Save the returned VE verification SID with the user and phone number that initiated the request, then use it to check the code entered by the user.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Obtain these credentials from the application's private configuration.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'BasicAuth', 1
EXEC sp_OASetProperty @http, 'Login', 'YOUR_API_KEY_SID'
EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_KEY_SECRET'
-- Both requests use the same Verify Service SID (VA...).
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://verify.twilio.com/v2/Services/'
EXEC sp_OAMethod @url, 'Append', @success OUT, 'YOUR_VERIFY_SERVICE_SID'
EXEC sp_OAMethod @url, 'Append', @success OUT, '/Verifications'
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
-- Supply the number the user asked to verify. AddParam encodes the leading plus sign.
EXEC sp_OAMethod @req, 'AddParam', NULL, 'To', 'PHONE_NUMBER_IN_E164_FORMAT'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'Channel', 'sms'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No response from Twilio. Check connectivity and TLS settings.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
DECLARE @parsed int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Twilio returned HTTP ' + @iTmp0
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'code'
IF (@parsed = 1) and (@iTmp0 = 2)
BEGIN
EXEC sp_OAMethod @result, 'IntOf', @iTmp0 OUT, 'code'
PRINT 'Twilio error code: ' + @iTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'status'
IF (@parsed <> 1) or (@iTmp0 <> 1)
BEGIN
PRINT 'Verification status missing from the response.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @status nvarchar(4000)
EXEC sp_OAMethod @result, 'StringOf', @status OUT, 'status'
IF @status <> 'pending'
BEGIN
PRINT 'Verification status: ' + @status
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'sid'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'sid'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'The pending response did not include a verification SID.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
-- Save this SID with the pending attempt before prompting the user for the code.
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'sid'
PRINT 'Pending verification SID: ' + @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
END
GO