Chilkat Online Tools

SQL Server / Twilio Messaging / SMS / Send an SMS

Back to Collection Items

-- 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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    EXEC sp_OASetProperty @http, 'Login', '{{TWILIO_ACCOUNT_SID}}'
    EXEC sp_OASetProperty @http, 'Password', '{{TWILIO_AUTH_TOKEN}}'

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.HttpRequest', @req OUT

    EXEC sp_OAMethod @req, 'AddParam', NULL, 'Body', 'Hello World!'

    DECLARE @jsonParam2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam2 OUT

    EXEC sp_OAMethod @jsonParam2, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'To', @sTmp0

    DECLARE @jsonParam3 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam3 OUT

    EXEC sp_OAMethod @jsonParam3, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'From', @sTmp0

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json', @req
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonParam2
        EXEC @hr = sp_OADestroy @jsonParam3
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonParam2
        EXEC @hr = sp_OADestroy @jsonParam3
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "account_sid": "ACF8",
    --   "api_version": "exercitation eiusmod amet laboris",
    --   "body": "consectetur exercitation officia magna",
    --   "date_created": "consectetur exercitation proident",
    --   "date_sent": "culpa sint labore",
    --   "date_updated": "proident",
    --   "direction": "outbound-call",
    --   "error_code": -64742646,
    --   "error_message": "nostrud sint Ut",
    --   "from": "Excepteur cillum aute",
    --   "messaging_service_sid": "MGDB",
    --   "num_media": "dolor veniam non",
    --   "num_segments": "et ut sint ut amet",
    --   "price": "Lorem reprehenderit",
    --   "price_unit": "Excepteur tempor i",
    --   "sid": "MM79",
    --   "status": "received",
    --   "subresource_uris": {},
    --   "to": "Ut non id",
    --   "uri": "magna sit esse"
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @date_created int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.DtObj', @date_created OUT

    DECLARE @date_sent int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.DtObj', @date_sent OUT

    DECLARE @date_updated int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.DtObj', @date_updated OUT

    DECLARE @account_sid nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @account_sid OUT, 'account_sid'
    DECLARE @api_version nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @api_version OUT, 'api_version'
    DECLARE @body nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @body OUT, 'body'
    EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'date_created', 0, @date_created
    EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'date_sent', 0, @date_sent
    EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'date_updated', 0, @date_updated
    DECLARE @direction nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @direction OUT, 'direction'
    DECLARE @error_code int
    EXEC sp_OAMethod @jResp, 'IntOf', @error_code OUT, 'error_code'
    DECLARE @error_message nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @error_message OUT, 'error_message'
    DECLARE @from nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @from OUT, 'from'
    DECLARE @messaging_service_sid nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @messaging_service_sid OUT, 'messaging_service_sid'
    DECLARE @num_media nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @num_media OUT, 'num_media'
    DECLARE @num_segments nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @num_segments OUT, 'num_segments'
    DECLARE @price nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @price OUT, 'price'
    DECLARE @price_unit nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @price_unit OUT, 'price_unit'
    DECLARE @sid nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @sid OUT, 'sid'
    DECLARE @status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @status OUT, 'status'
    DECLARE @v_to nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_to OUT, 'to'
    DECLARE @uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @uri OUT, 'uri'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @jsonParam2
    EXEC @hr = sp_OADestroy @jsonParam3
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp
    EXEC @hr = sp_OADestroy @date_created
    EXEC @hr = sp_OADestroy @date_sent
    EXEC @hr = sp_OADestroy @date_updated


END
GO

Curl Command

curl -X POST
	-u '{{TWILIO_ACCOUNT_SID}}:{{TWILIO_AUTH_TOKEN}}'
	-H "Content-Type: application/x-www-form-urlencoded"
	--data-urlencode 'Body=Hello World!'
	--data-urlencode 'To={{MY_PHONE_NUMBER}}'
	--data-urlencode 'From={{TWILIO_PHONE_NUMBER}}'
https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json

Postman Collection Item JSON

{
  "name": "Send an SMS",
  "request": {
    "auth": {
      "type": "basic",
      "basic": [
        {
          "key": "password",
          "value": "{{TWILIO_AUTH_TOKEN}}",
          "type": "string"
        },
        {
          "key": "username",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/x-www-form-urlencoded"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "Body",
          "value": "Hello World!",
          "description": "The text of the message you want to send. Can be up to 1,600 characters in length."
        },
        {
          "key": "To",
          "value": "{{MY_PHONE_NUMBER}}",
          "description": "The destination phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format for SMS/MMS or [Channel user address](https://www.twilio.com/docs/sms/channels#channel-addresses) for other 3rd-party channels."
        },
        {
          "key": "From",
          "value": "{{TWILIO_PHONE_NUMBER}}",
          "description": "A Twilio phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format, an [alphanumeric sender ID](https://www.twilio.com/docs/sms/send-messages#use-an-alphanumeric-sender-id), or a [Channel Endpoint address](https://www.twilio.com/docs/sms/channels#channel-addresses) that is enabled for the type of message you want to send. Phone numbers or [short codes](https://www.twilio.com/docs/sms/api/short-code) purchased from Twilio also work here. You cannot, for example, spoof messages from a private cell phone number. If you are using `messaging_service_sid`, this parameter must be empty."
        },
        {
          "key": "AddressRetention",
          "value": "<string>",
          "description": "Determines if the address can be stored or obfuscated based on privacy settings (This can only be one of retain)",
          "disabled": true
        },
        {
          "key": "ApplicationSid",
          "value": "<string>",
          "description": "The SID of the application that should receive message status. We POST a `message_sid` parameter and a `message_status` parameter with a value of `sent` or `failed` to the [application](https://www.twilio.com/docs/usage/api/applications)'s `message_status_callback`. If a `status_callback` parameter is also passed, it will be ignored and the application's `message_status_callback` parameter will be used.",
          "disabled": true
        },
        {
          "key": "Attempt",
          "value": "<integer>",
          "description": "Total number of attempts made ( including this ) to send out the message regardless of the provider used",
          "disabled": true
        },
        {
          "key": "ContentRetention",
          "value": "<string>",
          "description": "Determines if the message content can be stored or redacted based on privacy settings (This can only be one of retain)",
          "disabled": true
        },
        {
          "key": "ForceDelivery",
          "value": "<boolean>",
          "description": "Reserved",
          "disabled": true
        },
        {
          "key": "MaxPrice",
          "value": "<number>",
          "description": "The maximum total price in US dollars that you will pay for the message to be delivered. Can be a decimal value that has up to 4 decimal places. All messages are queued for delivery and the message cost is checked before the message is sent. If the cost exceeds `max_price`, the message will fail and a status of `Failed` is sent to the status callback. If `MaxPrice` is not set, the message cost is not checked.",
          "disabled": true
        },
        {
          "key": "MediaUrl",
          "value": "[\"<uri>\",\"<uri>\"]",
          "description": "The URL of the media to send with the message. The media can be of type `gif`, `png`, and `jpeg` and will be formatted correctly on the recipient's device. The media size limit is 5MB for supported file types (JPEG, PNG, GIF) and 500KB for [other types](https://www.twilio.com/docs/sms/accepted-mime-types) of accepted media. To send more than one image in the message body, provide multiple `media_url` parameters in the POST request. You can include up to 10 `media_url` parameters per message. You can send images in an SMS message in only the US and Canada.",
          "disabled": true
        },
        {
          "key": "MessagingServiceSid",
          "value": "<string>",
          "description": "The SID of the [Messaging Service](https://www.twilio.com/docs/sms/services#send-a-message-with-copilot) you want to associate with the Message. Set this parameter to use the [Messaging Service Settings and Copilot Features](https://www.twilio.com/console/sms/services) you have configured and leave the `from` parameter empty. When only this parameter is set, Twilio will use your enabled Copilot Features to select the `from` phone number for delivery.",
          "disabled": true
        },
        {
          "key": "PersistentAction",
          "value": "[\"<string>\",\"<string>\"]",
          "description": "Rich actions for Channels Messages.",
          "disabled": true
        },
        {
          "key": "ProvideFeedback",
          "value": "<boolean>",
          "description": "Whether to confirm delivery of the message. Set this value to `true` if you are sending messages that have a trackable user action and you intend to confirm delivery of the message using the [Message Feedback API](https://www.twilio.com/docs/sms/api/message-feedback-resource). This parameter is `false` by default.",
          "disabled": true
        },
        {
          "key": "SmartEncoded",
          "value": "<boolean>",
          "description": "Whether to detect Unicode characters that have a similar GSM-7 character and replace them. Can be: `true` or `false`.",
          "disabled": true
        },
        {
          "key": "StatusCallback",
          "value": "<uri>",
          "description": "The URL we should call using the `status_callback_method` to send status information to your application. If specified, we POST these message status changes to the URL: `queued`, `failed`, `sent`, `delivered`, or `undelivered`. Twilio will POST its [standard request parameters](https://www.twilio.com/docs/sms/twiml#request-parameters) as well as some additional parameters including `MessageSid`, `MessageStatus`, and `ErrorCode`. If you include this parameter with the `messaging_service_sid`, we use this URL instead of the Status Callback URL of the [Messaging Service](https://www.twilio.com/docs/sms/services/api). URLs must contain a valid hostname and underscores are not allowed.",
          "disabled": true
        },
        {
          "key": "ValidityPeriod",
          "value": "<integer>",
          "description": "How long in seconds the message can remain in our outgoing message queue. After this period elapses, the message fails and we call your status callback. Can be between 1 and the default value of 14,400 seconds. After a message has been accepted by a carrier, however, we cannot guarantee that the message will not be queued after this period. We recommend that this value be at least 5 seconds.",
          "disabled": true
        }
      ]
    },
    "url": {
      "raw": "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json",
      "host": [
        "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
      ],
      "path": [
        "2010-04-01",
        "Accounts",
        ":AccountSid",
        "Messages.json"
      ],
      "variable": [
        {
          "key": "AccountSid",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "description": "(Required) The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource."
        }
      ]
    },
    "description": "Send a message from the account used to make the request with the phone number you purchased from Twilio in the previous step or with any other Twilio phone number you have associated in your account.\n\nRead more about it in the [Twilio Docs](https://www.twilio.com/docs/sms/api/message-resource#create-a-message-resource)"
  },
  "response": [
    {
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "To",
              "value": "<string>",
              "description": "The destination phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format for SMS/MMS or [Channel user address](https://www.twilio.com/docs/sms/channels#channel-addresses) for other 3rd-party channels."
            },
            {
              "key": "AddressRetention",
              "value": "<string>",
              "description": "Determines if the address can be stored or obfuscated based on privacy settings (This can only be one of retain)"
            },
            {
              "key": "ApplicationSid",
              "value": "<string>",
              "description": "The SID of the application that should receive message status. We POST a `message_sid` parameter and a `message_status` parameter with a value of `sent` or `failed` to the [application](https://www.twilio.com/docs/usage/api/applications)'s `message_status_callback`. If a `status_callback` parameter is also passed, it will be ignored and the application's `message_status_callback` parameter will be used."
            },
            {
              "key": "Attempt",
              "value": "<integer>",
              "description": "Total number of attempts made ( including this ) to send out the message regardless of the provider used"
            },
            {
              "key": "Body",
              "value": "<string>",
              "description": "The text of the message you want to send. Can be up to 1,600 characters in length."
            },
            {
              "key": "ContentRetention",
              "value": "<string>",
              "description": "Determines if the message content can be stored or redacted based on privacy settings (This can only be one of retain)"
            },
            {
              "key": "ForceDelivery",
              "value": "<boolean>",
              "description": "Reserved"
            },
            {
              "key": "From",
              "value": "<string>",
              "description": "A Twilio phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format, an [alphanumeric sender ID](https://www.twilio.com/docs/sms/send-messages#use-an-alphanumeric-sender-id), or a [Channel Endpoint address](https://www.twilio.com/docs/sms/channels#channel-addresses) that is enabled for the type of message you want to send. Phone numbers or [short codes](https://www.twilio.com/docs/sms/api/short-code) purchased from Twilio also work here. You cannot, for example, spoof messages from a private cell phone number. If you are using `messaging_service_sid`, this parameter must be empty."
            },
            {
              "key": "MaxPrice",
              "value": "<number>",
              "description": "The maximum total price in US dollars that you will pay for the message to be delivered. Can be a decimal value that has up to 4 decimal places. All messages are queued for delivery and the message cost is checked before the message is sent. If the cost exceeds `max_price`, the message will fail and a status of `Failed` is sent to the status callback. If `MaxPrice` is not set, the message cost is not checked."
            },
            {
              "key": "MediaUrl",
              "value": "[\"<uri>\",\"<uri>\"]",
              "description": "The URL of the media to send with the message. The media can be of type `gif`, `png`, and `jpeg` and will be formatted correctly on the recipient's device. The media size limit is 5MB for supported file types (JPEG, PNG, GIF) and 500KB for [other types](https://www.twilio.com/docs/sms/accepted-mime-types) of accepted media. To send more than one image in the message body, provide multiple `media_url` parameters in the POST request. You can include up to 10 `media_url` parameters per message. You can send images in an SMS message in only the US and Canada."
            },
            {
              "key": "MessagingServiceSid",
              "value": "<string>",
              "description": "The SID of the [Messaging Service](https://www.twilio.com/docs/sms/services#send-a-message-with-copilot) you want to associate with the Message. Set this parameter to use the [Messaging Service Settings and Copilot Features](https://www.twilio.com/console/sms/services) you have configured and leave the `from` parameter empty. When only this parameter is set, Twilio will use your enabled Copilot Features to select the `from` phone number for delivery."
            },
            {
              "key": "PersistentAction",
              "value": "[\"<string>\",\"<string>\"]",
              "description": "Rich actions for Channels Messages."
            },
            {
              "key": "ProvideFeedback",
              "value": "<boolean>",
              "description": "Whether to confirm delivery of the message. Set this value to `true` if you are sending messages that have a trackable user action and you intend to confirm delivery of the message using the [Message Feedback API](https://www.twilio.com/docs/sms/api/message-feedback-resource). This parameter is `false` by default."
            },
            {
              "key": "SmartEncoded",
              "value": "<boolean>",
              "description": "Whether to detect Unicode characters that have a similar GSM-7 character and replace them. Can be: `true` or `false`."
            },
            {
              "key": "StatusCallback",
              "value": "<uri>",
              "description": "The URL we should call using the `status_callback_method` to send status information to your application. If specified, we POST these message status changes to the URL: `queued`, `failed`, `sent`, `delivered`, or `undelivered`. Twilio will POST its [standard request parameters](https://www.twilio.com/docs/sms/twiml#request-parameters) as well as some additional parameters including `MessageSid`, `MessageStatus`, and `ErrorCode`. If you include this parameter with the `messaging_service_sid`, we use this URL instead of the Status Callback URL of the [Messaging Service](https://www.twilio.com/docs/sms/services/api). URLs must contain a valid hostname and underscores are not allowed."
            },
            {
              "key": "ValidityPeriod",
              "value": "<integer>",
              "description": "How long in seconds the message can remain in our outgoing message queue. After this period elapses, the message fails and we call your status callback. Can be between 1 and the default value of 14,400 seconds. After a message has been accepted by a carrier, however, we cannot guarantee that the message will not be queued after this period. We recommend that this value be at least 5 seconds."
            }
          ]
        },
        "url": {
          "raw": "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json",
          "host": [
            "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
          ],
          "path": [
            "2010-04-01",
            "Accounts",
            ":AccountSid",
            "Messages.json"
          ],
          "variable": [
            {
              "key": "AccountSid"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"account_sid\": \"ACF8\",\n \"api_version\": \"exercitation eiusmod amet laboris\",\n \"body\": \"consectetur exercitation officia magna\",\n \"date_created\": \"consectetur exercitation proident\",\n \"date_sent\": \"culpa sint labore\",\n \"date_updated\": \"proident\",\n \"direction\": \"outbound-call\",\n \"error_code\": -64742646,\n \"error_message\": \"nostrud sint Ut\",\n \"from\": \"Excepteur cillum aute\",\n \"messaging_service_sid\": \"MGDB\",\n \"num_media\": \"dolor veniam non\",\n \"num_segments\": \"et ut sint ut amet\",\n \"price\": \"Lorem reprehenderit\",\n \"price_unit\": \"Excepteur tempor i\",\n \"sid\": \"MM79\",\n \"status\": \"received\",\n \"subresource_uris\": {},\n \"to\": \"Ut non id\",\n \"uri\": \"magna sit esse\"\n}"
    }
  ]
}