Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Send an SMS with Twilio

See more Twilio Examples

Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.

For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.

Twilio Messages API reference

Chilkat SQL Server Downloads

SQL Server
-- 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.
    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
    --  Use the test Account SID and test Auth Token from the Twilio Console.
    DECLARE @accountSid nvarchar(4000)
    SELECT @accountSid = 'YOUR_TEST_ACCOUNT_SID'
    EXEC sp_OASetProperty @http, 'Login', @accountSid
    EXEC sp_OASetProperty @http, 'Password', 'YOUR_TEST_AUTH_TOKEN'
    DECLARE @url int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT

    EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://api.twilio.com/2010-04-01/Accounts/'
    EXEC sp_OAMethod @url, 'Append', @success OUT, @accountSid
    EXEC sp_OAMethod @url, 'Append', @success OUT, '/Messages.json'

    --  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
    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'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'From', '+15005550006'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'To', '+12025550123'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'Body', 'Hello from Twilio!'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @reply int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply 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
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @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 @reply
        RETURN
      END
    DECLARE @parsed int
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @reply, 'Load', @parsed OUT, @sTmp0
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF (@success <> 1) or (@iTmp0 <> 201)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'HTTP ' + @iTmp0
        EXEC sp_OAMethod @reply, 'HasMember', @iTmp0 OUT, 'code'
        IF (@parsed = 1) and (@iTmp0 = 1)
          BEGIN

            EXEC sp_OAMethod @reply, 'IntOf', @iTmp0 OUT, 'code'

            EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'message'
            PRINT 'Twilio error ' + @iTmp0 + ': ' + @sTmp0
          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 @reply
        RETURN
      END
    IF @parsed <> 1
      BEGIN

        PRINT 'Expected a JSON 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 @reply
        RETURN
      END
    EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'sid'
    EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp1 OUT, 'status'
    IF (@iTmp0 <> 1) or (@iTmp1 <> 1)
      BEGIN

        PRINT 'The create response is missing the message SID or 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 @reply
        RETURN
      END

    EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'sid'
    PRINT 'Message SID: ' + @sTmp0

    EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'status'
    PRINT 'Initial status: ' + @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 @reply


END
GO