Chilkat Online Tools

SQL Server / Twilio Messaging / SMS / Find available phone numbers

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
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.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 @queryParams int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'SmsEnabled', 'true'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://api.twilio.com/2010-04-01/Accounts/:AccountSid/AvailablePhoneNumbers/:CountryCode/Local.json', @queryParams
    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 @queryParams
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

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

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.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 @queryParams
        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)

    -- {
    --   "available_phone_numbers": [
    --     {
    --       "address_requirements": "aute eiusmod",
    --       "beta": true,
    --       "capabilities": {},
    --       "friendly_name": "nisi do",
    --       "iso_country": "voluptate",
    --       "lata": "minim cillum",
    --       "latitude": 4649799.109687477,
    --       "locality": "in Duis ut anim irure",
    --       "longitude": -94614168.65959848,
    --       "phone_number": "amet labore",
    --       "postal_code": "deserunt esse cillum ut",
    --       "rate_center": "velit",
    --       "region": "ipsum pariatur cupidatat dolor"
    --     },
    --     {
    --       "address_requirements": "Ut",
    --       "beta": true,
    --       "capabilities": {},
    --       "friendly_name": "ullamco",
    --       "iso_country": "in incididunt",
    --       "lata": "cillum dolor velit sed",
    --       "latitude": -21235506.84035504,
    --       "locality": "sint",
    --       "longitude": -25668838.90140347,
    --       "phone_number": "non adipisicing enim incididunt deserunt",
    --       "postal_code": "ut voluptate minim",
    --       "rate_center": "velit aliqua",
    --       "region": "Excepteur aliquip in nisi"
    --     }
    --   ],
    --   "end": 3504435,
    --   "first_page_uri": "http://nnYdJBuOCqryvmKnbvBPBnaNMbCqrxw.tdvyHFVoZISsFUqJvqFT0cg+",
    --   "next_page_uri": "http://PoqRnDNQHgsSAILsyYutOtNGDj.qqhjm8TRty6qyZnDJAQcH1Djqzpuv,w5pf+hzR.4snyBz",
    --   "page": 90555719,
    --   "page_size": 88924560,
    --   "previous_page_uri": "https://ArZjnsEHjpSMJWeIOQAYyTOm.ispdr0ZozBph6JkyS4wCN4KsLijal5vGd-yowauXTc55dxOb",
    --   "start": -60781383,
    --   "uri": "http://BJmswSvlkWlnMst.fiODjnzufdg.eBwAEpkSmwiU.e-Ygm,OFGiH"
    -- }

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

    DECLARE @address_requirements nvarchar(4000)

    DECLARE @beta int

    DECLARE @friendly_name nvarchar(4000)

    DECLARE @iso_country nvarchar(4000)

    DECLARE @lata nvarchar(4000)

    DECLARE @latitude nvarchar(4000)

    DECLARE @locality nvarchar(4000)

    DECLARE @longitude nvarchar(4000)

    DECLARE @phone_number nvarchar(4000)

    DECLARE @postal_code nvarchar(4000)

    DECLARE @rate_center nvarchar(4000)

    DECLARE @region nvarchar(4000)

    DECLARE @v_end int
    EXEC sp_OAMethod @jResp, 'IntOf', @v_end OUT, 'end'
    DECLARE @first_page_uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @first_page_uri OUT, 'first_page_uri'
    DECLARE @next_page_uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @next_page_uri OUT, 'next_page_uri'
    DECLARE @page int
    EXEC sp_OAMethod @jResp, 'IntOf', @page OUT, 'page'
    DECLARE @page_size int
    EXEC sp_OAMethod @jResp, 'IntOf', @page_size OUT, 'page_size'
    DECLARE @previous_page_uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @previous_page_uri OUT, 'previous_page_uri'
    DECLARE @start int
    EXEC sp_OAMethod @jResp, 'IntOf', @start OUT, 'start'
    DECLARE @uri nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @uri OUT, 'uri'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'available_phone_numbers'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @address_requirements OUT, 'available_phone_numbers[i].address_requirements'
        EXEC sp_OAMethod @jResp, 'BoolOf', @beta OUT, 'available_phone_numbers[i].beta'
        EXEC sp_OAMethod @jResp, 'StringOf', @friendly_name OUT, 'available_phone_numbers[i].friendly_name'
        EXEC sp_OAMethod @jResp, 'StringOf', @iso_country OUT, 'available_phone_numbers[i].iso_country'
        EXEC sp_OAMethod @jResp, 'StringOf', @lata OUT, 'available_phone_numbers[i].lata'
        EXEC sp_OAMethod @jResp, 'StringOf', @latitude OUT, 'available_phone_numbers[i].latitude'
        EXEC sp_OAMethod @jResp, 'StringOf', @locality OUT, 'available_phone_numbers[i].locality'
        EXEC sp_OAMethod @jResp, 'StringOf', @longitude OUT, 'available_phone_numbers[i].longitude'
        EXEC sp_OAMethod @jResp, 'StringOf', @phone_number OUT, 'available_phone_numbers[i].phone_number'
        EXEC sp_OAMethod @jResp, 'StringOf', @postal_code OUT, 'available_phone_numbers[i].postal_code'
        EXEC sp_OAMethod @jResp, 'StringOf', @rate_center OUT, 'available_phone_numbers[i].rate_center'
        EXEC sp_OAMethod @jResp, 'StringOf', @region OUT, 'available_phone_numbers[i].region'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @queryParams
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -G -d "SmsEnabled=true"
	-u '{{TWILIO_ACCOUNT_SID}}:{{TWILIO_AUTH_TOKEN}}'
https://api.twilio.com/2010-04-01/Accounts/:AccountSid/AvailablePhoneNumbers/:CountryCode/Local.json

Postman Collection Item JSON

{
  "name": "Find available phone numbers",
  "request": {
    "auth": {
      "type": "basic",
      "basic": [
        {
          "key": "password",
          "value": "{{TWILIO_AUTH_TOKEN}}",
          "type": "string"
        },
        {
          "key": "username",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "type": "string"
        }
      ]
    },
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "{{2010-04-01-Accounts-AccountSid-AvailablePhoneNumbers-CountryCode-Local.json-Url}}/2010-04-01/Accounts/:AccountSid/AvailablePhoneNumbers/:CountryCode/Local.json?SmsEnabled=true",
      "host": [
        "{{2010-04-01-Accounts-AccountSid-AvailablePhoneNumbers-CountryCode-Local.json-Url}}"
      ],
      "path": [
        "2010-04-01",
        "Accounts",
        ":AccountSid",
        "AvailablePhoneNumbers",
        ":CountryCode",
        "Local.json"
      ],
      "query": [
        {
          "key": "AreaCode",
          "value": "<integer>",
          "description": "The area code of the phone numbers to read. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "Contains",
          "value": "<string>",
          "description": "The pattern on which to match phone numbers. Valid characters are `*`, `0-9`, `a-z`, and `A-Z`. The `*` character matches any single digit. For examples, see [Example 2](https://www.twilio.com/docs/phone-numbers/api/availablephonenumberlocal-resource?code-sample=code-find-phone-numbers-by-number-pattern) and [Example 3](https://www.twilio.com/docs/phone-numbers/api/availablephonenumberlocal-resource?code-sample=code-find-phone-numbers-by-character-pattern). If specified, this value must have at least two characters.",
          "disabled": true
        },
        {
          "key": "SmsEnabled",
          "value": "true",
          "description": "Whether the phone numbers can receive text messages. Can be: `true` or `false`."
        },
        {
          "key": "MmsEnabled",
          "value": "<boolean>",
          "description": "Whether the phone numbers can receive MMS messages. Can be: `true` or `false`.",
          "disabled": true
        },
        {
          "key": "VoiceEnabled",
          "value": "<boolean>",
          "description": "Whether the phone numbers can receive calls. Can be: `true` or `false`.",
          "disabled": true
        },
        {
          "key": "ExcludeAllAddressRequired",
          "value": "<boolean>",
          "description": "Whether to exclude phone numbers that require an [Address](https://www.twilio.com/docs/usage/api/address). Can be: `true` or `false` and the default is `false`.",
          "disabled": true
        },
        {
          "key": "ExcludeLocalAddressRequired",
          "value": "<boolean>",
          "description": "Whether to exclude phone numbers that require a local [Address](https://www.twilio.com/docs/usage/api/address). Can be: `true` or `false` and the default is `false`.",
          "disabled": true
        },
        {
          "key": "ExcludeForeignAddressRequired",
          "value": "<boolean>",
          "description": "Whether to exclude phone numbers that require a foreign [Address](https://www.twilio.com/docs/usage/api/address). Can be: `true` or `false` and the default is `false`.",
          "disabled": true
        },
        {
          "key": "Beta",
          "value": "<boolean>",
          "description": "Whether to read phone numbers that are new to the Twilio platform. Can be: `true` or `false` and the default is `true`.",
          "disabled": true
        },
        {
          "key": "NearNumber",
          "value": "<string>",
          "description": "Given a phone number, find a geographically close number within `distance` miles. Distance defaults to 25 miles. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "NearLatLong",
          "value": "<string>",
          "description": "Given a latitude/longitude pair `lat,long` find geographically close numbers within `distance` miles. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "Distance",
          "value": "<integer>",
          "description": "The search radius, in miles, for a `near_` query.  Can be up to `500` and the default is `25`. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "InPostalCode",
          "value": "<string>",
          "description": "Limit results to a particular postal code. Given a phone number, search within the same postal code as that number. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "InRegion",
          "value": "<string>",
          "description": "Limit results to a particular region, state, or province. Given a phone number, search within the same region as that number. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "InRateCenter",
          "value": "<string>",
          "description": "Limit results to a specific rate center, or given a phone number search within the same rate center as that number. Requires `in_lata` to be set as well. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "InLata",
          "value": "<string>",
          "description": "Limit results to a specific local access and transport area ([LATA](https://en.wikipedia.org/wiki/Local_access_and_transport_area)). Given a phone number, search within the same [LATA](https://en.wikipedia.org/wiki/Local_access_and_transport_area) as that number. Applies to only phone numbers in the US and Canada.",
          "disabled": true
        },
        {
          "key": "InLocality",
          "value": "<string>",
          "description": "Limit results to a particular locality or city. Given a phone number, search within the same Locality as that number.",
          "disabled": true
        },
        {
          "key": "FaxEnabled",
          "value": "<boolean>",
          "description": "Whether the phone numbers can receive faxes. Can be: `true` or `false`.",
          "disabled": true
        },
        {
          "key": "PageSize",
          "value": "<integer>",
          "description": "How many resources to return in each list page. The default is 50, and the maximum is 1000.",
          "disabled": true
        }
      ],
      "variable": [
        {
          "key": "AccountSid",
          "value": "{{TWILIO_ACCOUNT_SID}}",
          "description": "(Required) The SID of the [Account](https://www.twilio.com/docs/iam/api/account) requesting the AvailablePhoneNumber resources."
        },
        {
          "key": "CountryCode",
          "value": "US",
          "description": "(Required) The [ISO-3166-1](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) country code of the country from which to read phone numbers."
        }
      ]
    },
    "description": "To send an SMS with Twilio you'll first need to buy a phone number. For that you have to search the catalog of available phone numbers.\n\nOnce you have the list of available phone numbers, pick one and use it in the next request to buy the number.\n\nLearn more about this in the [Twilio Docs](https://www.twilio.com/docs/phone-numbers/api/availablephonenumberlocal-resource)."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "url": {
          "raw": "{{2010-04-01-Accounts-AccountSid-AvailablePhoneNumbers-CountryCode-Local.json-Url}}/2010-04-01/Accounts/:AccountSid/AvailablePhoneNumbers/:CountryCode/Local.json?AreaCode=<integer>&Contains=<string>&SmsEnabled=<boolean>&MmsEnabled=<boolean>&VoiceEnabled=<boolean>&ExcludeAllAddressRequired=<boolean>&ExcludeLocalAddressRequired=<boolean>&ExcludeForeignAddressRequired=<boolean>&Beta=<boolean>&NearNumber=<string>&NearLatLong=<string>&Distance=<integer>&InPostalCode=<string>&InRegion=<string>&InRateCenter=<string>&InLata=<string>&InLocality=<string>&FaxEnabled=<boolean>&PageSize=<integer>",
          "host": [
            "{{2010-04-01-Accounts-AccountSid-AvailablePhoneNumbers-CountryCode-Local.json-Url}}"
          ],
          "path": [
            "2010-04-01",
            "Accounts",
            ":AccountSid",
            "AvailablePhoneNumbers",
            ":CountryCode",
            "Local.json"
          ],
          "query": [
            {
              "key": "AreaCode",
              "value": "<integer>"
            },
            {
              "key": "Contains",
              "value": "<string>"
            },
            {
              "key": "SmsEnabled",
              "value": "<boolean>"
            },
            {
              "key": "MmsEnabled",
              "value": "<boolean>"
            },
            {
              "key": "VoiceEnabled",
              "value": "<boolean>"
            },
            {
              "key": "ExcludeAllAddressRequired",
              "value": "<boolean>"
            },
            {
              "key": "ExcludeLocalAddressRequired",
              "value": "<boolean>"
            },
            {
              "key": "ExcludeForeignAddressRequired",
              "value": "<boolean>"
            },
            {
              "key": "Beta",
              "value": "<boolean>"
            },
            {
              "key": "NearNumber",
              "value": "<string>"
            },
            {
              "key": "NearLatLong",
              "value": "<string>"
            },
            {
              "key": "Distance",
              "value": "<integer>"
            },
            {
              "key": "InPostalCode",
              "value": "<string>"
            },
            {
              "key": "InRegion",
              "value": "<string>"
            },
            {
              "key": "InRateCenter",
              "value": "<string>"
            },
            {
              "key": "InLata",
              "value": "<string>"
            },
            {
              "key": "InLocality",
              "value": "<string>"
            },
            {
              "key": "FaxEnabled",
              "value": "<boolean>"
            },
            {
              "key": "PageSize",
              "value": "<integer>"
            }
          ],
          "variable": [
            {
              "key": "AccountSid"
            },
            {
              "key": "CountryCode"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"available_phone_numbers\": [\n  {\n   \"address_requirements\": \"aute eiusmod\",\n   \"beta\": true,\n   \"capabilities\": {},\n   \"friendly_name\": \"nisi do\",\n   \"iso_country\": \"voluptate\",\n   \"lata\": \"minim cillum\",\n   \"latitude\": 4649799.109687477,\n   \"locality\": \"in Duis ut anim irure\",\n   \"longitude\": -94614168.65959848,\n   \"phone_number\": \"amet labore\",\n   \"postal_code\": \"deserunt esse cillum ut\",\n   \"rate_center\": \"velit\",\n   \"region\": \"ipsum pariatur cupidatat dolor\"\n  },\n  {\n   \"address_requirements\": \"Ut\",\n   \"beta\": true,\n   \"capabilities\": {},\n   \"friendly_name\": \"ullamco\",\n   \"iso_country\": \"in incididunt\",\n   \"lata\": \"cillum dolor velit sed\",\n   \"latitude\": -21235506.84035504,\n   \"locality\": \"sint\",\n   \"longitude\": -25668838.90140347,\n   \"phone_number\": \"non adipisicing enim incididunt deserunt\",\n   \"postal_code\": \"ut voluptate minim\",\n   \"rate_center\": \"velit aliqua\",\n   \"region\": \"Excepteur aliquip in nisi\"\n  }\n ],\n \"end\": 3504435,\n \"first_page_uri\": \"http://nnYdJBuOCqryvmKnbvBPBnaNMbCqrxw.tdvyHFVoZISsFUqJvqFT0cg+\",\n \"next_page_uri\": \"http://PoqRnDNQHgsSAILsyYutOtNGDj.qqhjm8TRty6qyZnDJAQcH1Djqzpuv,w5pf+hzR.4snyBz\",\n \"page\": 90555719,\n \"page_size\": 88924560,\n \"previous_page_uri\": \"https://ArZjnsEHjpSMJWeIOQAYyTOm.ispdr0ZozBph6JkyS4wCN4KsLijal5vGd-yowauXTc55dxOb\",\n \"start\": -60781383,\n \"uri\": \"http://BJmswSvlkWlnMst.fiODjnzufdg.eBwAEpkSmwiU.e-Ygm,OFGiH\"\n}"
    }
  ]
}