Chilkat Online Tools

SQL Server / Plivo REST API / Search 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
    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', '{{auth_id}}'
    EXEC sp_OASetProperty @http, 'Password', 'password'

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

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'country_iso', 'US'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'type', 'local'
    EXEC sp_OAMethod @queryParams, 'UpdateInt', @success OUT, 'limit', 3

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://api.plivo.com/v1/Account/<auth_id>/PhoneNumber/', @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
    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 @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)

    -- {
    --   "api_id": "1a75dad2-5ef4-11eb-91fe-0242ac110003",
    --   "meta": {
    --     "limit": 3,
    --     "next": "/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/?limit=3&offset=3&type=local&country_iso=US",
    --     "offset": 0,
    --     "previous": null,
    --     "total_count": 14227
    --   },
    --   "objects": [
    --     {
    --       "city": "Saddleback Valley",
    --       "compliance_requirement": {
    --         "business": null,
    --         "individual": null
    --       },
    --       "country": "UNITED STATES",
    --       "lata": 730,
    --       "mms_enabled": true,
    --       "mms_rate": "0.00400",
    --       "monthly_rental_rate": "0.18000",
    --       "number": "19496031645",
    --       "prefix": "949",
    --       "prerequisites": [
    --       ],
    --       "rate_center": "Saddleback Valley",
    --       "region": "California",
    --       "resource_uri": "/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/19496031645/",
    --       "restriction": "",
    --       "restriction_text": "",
    --       "setup_rate": "0.00000",
    --       "sms_enabled": true,
    --       "sms_rate": "0.00000",
    --       "sub_type": "local",
    --       "type": "fixed",
    --       "voice_enabled": true,
    --       "voice_rate": "0.00300"
    --     },
    --     {
    --       "city": "Elwin",
    --       "compliance_requirement": {
    --         "business": null,
    --         "individual": null
    --       },
    --       "country": "UNITED STATES",
    --       "lata": 374,
    --       "mms_enabled": true,
    --       "mms_rate": "0.00400",
    --       "monthly_rental_rate": "0.18000",
    --       "number": "12177674383",
    --       "prefix": "217",
    --       "prerequisites": [
    --       ],
    --       "rate_center": "Elwin",
    --       "region": "Illinois",
    --       "resource_uri": "/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/12177674383/",
    --       "restriction": "",
    --       "restriction_text": "",
    --       "setup_rate": "0.00000",
    --       "sms_enabled": true,
    --       "sms_rate": "0.00000",
    --       "sub_type": "local",
    --       "type": "fixed",
    --       "voice_enabled": true,
    --       "voice_rate": "0.00300"
    --     },
    --     {
    --       "city": "Westwood",
    --       "compliance_requirement": {
    --         "business": null,
    --         "individual": null
    --       },
    --       "country": "UNITED STATES",
    --       "lata": 224,
    --       "mms_enabled": true,
    --       "mms_rate": "0.00400",
    --       "monthly_rental_rate": "0.18000",
    --       "number": "15513105271",
    --       "prefix": "551",
    --       "prerequisites": [
    --       ],
    --       "rate_center": "Westwood",
    --       "region": "New Jersey",
    --       "resource_uri": "/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/15513105271/",
    --       "restriction": "",
    --       "restriction_text": "",
    --       "setup_rate": "0.00000",
    --       "sms_enabled": true,
    --       "sms_rate": "0.00000",
    --       "sub_type": "local",
    --       "type": "fixed",
    --       "voice_enabled": true,
    --       "voice_rate": "0.00300"
    --     }
    --   ]
    -- }

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

    DECLARE @city nvarchar(4000)

    DECLARE @Business nvarchar(4000)

    DECLARE @Individual nvarchar(4000)

    DECLARE @country nvarchar(4000)

    DECLARE @lata int

    DECLARE @mms_enabled int

    DECLARE @mms_rate nvarchar(4000)

    DECLARE @monthly_rental_rate nvarchar(4000)

    DECLARE @v_number nvarchar(4000)

    DECLARE @prefix nvarchar(4000)

    DECLARE @rate_center nvarchar(4000)

    DECLARE @region nvarchar(4000)

    DECLARE @resource_uri nvarchar(4000)

    DECLARE @restriction nvarchar(4000)

    DECLARE @restriction_text nvarchar(4000)

    DECLARE @setup_rate nvarchar(4000)

    DECLARE @sms_enabled int

    DECLARE @sms_rate nvarchar(4000)

    DECLARE @sub_type nvarchar(4000)

    DECLARE @v_type nvarchar(4000)

    DECLARE @voice_enabled int

    DECLARE @voice_rate nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @api_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @api_id OUT, 'api_id'
    DECLARE @Limit int
    EXEC sp_OAMethod @jResp, 'IntOf', @Limit OUT, 'meta.limit'
    DECLARE @v_Next nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Next OUT, 'meta.next'
    DECLARE @Offset int
    EXEC sp_OAMethod @jResp, 'IntOf', @Offset OUT, 'meta.offset'
    DECLARE @Previous nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Previous OUT, 'meta.previous'
    DECLARE @Total_count int
    EXEC sp_OAMethod @jResp, 'IntOf', @Total_count OUT, 'meta.total_count'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'objects'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @city OUT, 'objects[i].city'
        EXEC sp_OAMethod @jResp, 'StringOf', @Business OUT, 'objects[i].compliance_requirement.business'
        EXEC sp_OAMethod @jResp, 'StringOf', @Individual OUT, 'objects[i].compliance_requirement.individual'
        EXEC sp_OAMethod @jResp, 'StringOf', @country OUT, 'objects[i].country'
        EXEC sp_OAMethod @jResp, 'IntOf', @lata OUT, 'objects[i].lata'
        EXEC sp_OAMethod @jResp, 'BoolOf', @mms_enabled OUT, 'objects[i].mms_enabled'
        EXEC sp_OAMethod @jResp, 'StringOf', @mms_rate OUT, 'objects[i].mms_rate'
        EXEC sp_OAMethod @jResp, 'StringOf', @monthly_rental_rate OUT, 'objects[i].monthly_rental_rate'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_number OUT, 'objects[i].number'
        EXEC sp_OAMethod @jResp, 'StringOf', @prefix OUT, 'objects[i].prefix'
        EXEC sp_OAMethod @jResp, 'StringOf', @rate_center OUT, 'objects[i].rate_center'
        EXEC sp_OAMethod @jResp, 'StringOf', @region OUT, 'objects[i].region'
        EXEC sp_OAMethod @jResp, 'StringOf', @resource_uri OUT, 'objects[i].resource_uri'
        EXEC sp_OAMethod @jResp, 'StringOf', @restriction OUT, 'objects[i].restriction'
        EXEC sp_OAMethod @jResp, 'StringOf', @restriction_text OUT, 'objects[i].restriction_text'
        EXEC sp_OAMethod @jResp, 'StringOf', @setup_rate OUT, 'objects[i].setup_rate'
        EXEC sp_OAMethod @jResp, 'BoolOf', @sms_enabled OUT, 'objects[i].sms_enabled'
        EXEC sp_OAMethod @jResp, 'StringOf', @sms_rate OUT, 'objects[i].sms_rate'
        EXEC sp_OAMethod @jResp, 'StringOf', @sub_type OUT, 'objects[i].sub_type'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'objects[i].type'
        EXEC sp_OAMethod @jResp, 'BoolOf', @voice_enabled OUT, 'objects[i].voice_enabled'
        EXEC sp_OAMethod @jResp, 'StringOf', @voice_rate OUT, 'objects[i].voice_rate'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'objects[i].prerequisites'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        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 "country_iso=US"
	-d "type=local"
	-d "limit=3"
	-u '{{auth_id}}:password'
https://api.plivo.com/v1/Account/<auth_id>/PhoneNumber/

Postman Collection Item JSON

{
  "name": "Search Phone Numbers",
  "protocolProfileBehavior": {
    "disableBodyPruning": true
  },
  "request": {
    "method": "GET",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.plivo.com/v1/Account/{{auth_id}}/PhoneNumber/?country_iso=US&type=local&limit=3",
      "protocol": "https",
      "host": [
        "api",
        "plivo",
        "com"
      ],
      "path": [
        "v1",
        "Account",
        "{{auth_id}}",
        "PhoneNumber",
        ""
      ],
      "query": [
        {
          "key": "country_iso",
          "value": "US",
          "description": "The ISO country code of the country."
        },
        {
          "key": "type",
          "value": "local",
          "description": "Filters by the type of the phone number. Possible values are tollfree, local, mobile, national and fixed."
        },
        {
          "key": "pattern",
          "value": "615",
          "description": "A pattern to match the phone number with. Phone numbers starting with (country calling code + pattern) are filtered in.",
          "disabled": true
        },
        {
          "key": "region",
          "value": "California",
          "description": "You could use the exact name of the region, for instance region=Frankfurt for a phone number in Frankfurt.",
          "disabled": true
        },
        {
          "key": "services",
          "value": "voice",
          "description": "Filters phone numbers which provide the selected services such as voice, sms & mms",
          "disabled": true
        },
        {
          "key": "city",
          "value": "New York",
          "description": "Filter phone number based on the city name. This filter is only applicable when the type is Local.",
          "disabled": true
        },
        {
          "key": "lata",
          "value": "722",
          "description": "Filters phone numbers in a LATA. This filter is applicable only for US and Canada",
          "disabled": true
        },
        {
          "key": "rate_center",
          "value": "310615",
          "description": "Filters phone numbers based on the rate center. This filter is applicable only for US and Canada.",
          "disabled": true
        },
        {
          "key": "limit",
          "value": "3",
          "description": "A limit on the number phone numbers to be returned. limit can range between 1 and 20, and the default is 20."
        },
        {
          "key": "offset",
          "value": "0",
          "description": "Denotes the number of value items by which the results should be offset. Defaults to 0",
          "disabled": true
        }
      ]
    },
    "description": "Returns a list of phone numbers that are available for purchase. You can search for tollfree, mobile, local, national and fixed phone numbers. The API allows you to filter phone numbers based on the country_iso, pattern, type and region.\n\nBelow is the list of arguments that can be passed in this API request, more information can be found [here](https://www.plivo.com/docs/numbers/api/phone-number#search-phone-numbers)\n\n| Arguments   | Description | Required/Conditional/Optional     |\n| :---        |    :----:   |          ---: |\n| country_iso      | The [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) of the country. For more information on supported countries, refer to our incoming [Voice](https://www.plivo.com/voice/coverage/us/) and [SMS](https://www.plivo.com/sms/coverage/us/) coverage.| Required  |\n| type      | Filters by the type of the phone number. Possible values are tollfree, local, mobile, national and fixed.| Optional  |\n| pattern      | A pattern to match the phone number with. Phone numbers starting with (country calling code + pattern) are filtered in. The allowed values are A-Z, 0-9, *, and ?.For example, in order to search for phone numbers in the US starting with 415 prefix, Pattern = 415. Filtered results will be of form '1415XXXXXXX'.| Optional  |\n| region      | This filter is only applicable when the type is fixed. If the type is not provided, it is assumed to be fixed. You could use the exact name of the region, for instance region=Frankfurt for a phone number in Frankfurt.| Optional  |\n| services      | Filters phone numbers which provide the selected services . Possible values are:<br />voice: Indicates that phone numbers which can receive calls are to be returned.<br />sms: Indicates that phone numbers which can receive SMS are to be returned.<br />mms: Indicates that phone numbers that can receive MMS are to be returned.<br />voice,sms: Indicates that phone numbers which can receive both calls and SMS are to be returned.<br />voice,sms,mms: Indicates that phone numbers that can receive calls, SMS and MMS are to be returned.| Optional  |\n| city      | Filter phone number based on the city name. This filter is only applicable when the type is Local.| Optional  |\n| lata      | Filters phone numbers in a [LATA](https://en.wikipedia.org/wiki/Local_access_and_transport_area). This filter is applicable only for US and Canada.| Optional  |\n| rate_center      | Filters phone numbers based on the [rate center](https://www.plivo.com/docs/numbers/api/phone-number/). This filter is applicable only for US and Canada.| Optional  |\n| limit | A limit on the number phone numbers to be returned. limit can range between 1 and 20, and the default is 20.| Optional  |\n| offset | Denotes the number of value items by which the results should be offset. Defaults to 0. Read more about offset based pagination [here](https://www.plivo.com/docs/numbers/api/request/pagination/).| Optional  |"
  },
  "response": [
    {
      "name": "Search Phone Numbers",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.plivo.com/v1/Account/{{auth_id}}/PhoneNumber/?country_iso=US&type=local&limit=3",
          "protocol": "https",
          "host": [
            "api",
            "plivo",
            "com"
          ],
          "path": [
            "v1",
            "Account",
            "{{auth_id}}",
            "PhoneNumber",
            ""
          ],
          "query": [
            {
              "key": "country_iso",
              "value": "US",
              "description": "The ISO country code of the country."
            },
            {
              "key": "type",
              "value": "local",
              "description": "Filters by the type of the phone number. Possible values are tollfree, local, mobile, national and fixed."
            },
            {
              "key": "pattern",
              "value": "615",
              "description": "A pattern to match the phone number with. Phone numbers starting with (country calling code + pattern) are filtered in.",
              "disabled": true
            },
            {
              "key": "region",
              "value": "California",
              "description": "You could use the exact name of the region, for instance region=Frankfurt for a phone number in Frankfurt.",
              "disabled": true
            },
            {
              "key": "services",
              "value": "voice",
              "description": "Filters phone numbers which provide the selected services such as voice, sms & mms",
              "disabled": true
            },
            {
              "key": "city",
              "value": "New York",
              "description": "Filter phone number based on the city name. This filter is only applicable when the type is Local.",
              "disabled": true
            },
            {
              "key": "lata",
              "value": "722",
              "description": "Filters phone numbers in a LATA. This filter is applicable only for US and Canada",
              "disabled": true
            },
            {
              "key": "rate_center",
              "value": "310615",
              "description": "Filters phone numbers based on the rate center. This filter is applicable only for US and Canada.",
              "disabled": true
            },
            {
              "key": "limit",
              "value": "3",
              "description": "A limit on the number phone numbers to be returned. limit can range between 1 and 20, and the default is 20."
            },
            {
              "key": "offset",
              "value": "0",
              "description": "Denotes the number of value items by which the results should be offset. Defaults to 0",
              "disabled": true
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 25 Jan 2021 10:00:07 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Content-Length",
          "value": "2570"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "X-Plivo-Signature",
          "value": "rFbFXY06oMSqyKlhf3Q/V7gady8="
        },
        {
          "key": "X-Plivo-Signature-V2",
          "value": "sYe601dE/b7eGEzwEiXOW1ygErn3SFdEmNxu6/hNubw="
        },
        {
          "key": "X-Plivo-Signature-MA-V2",
          "value": "sYe601dE/b7eGEzwEiXOW1ygErn3SFdEmNxu6/hNubw="
        },
        {
          "key": "X-Plivo-Signature-V2-Nonce",
          "value": "98286916815782313309"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"api_id\": \"1a75dad2-5ef4-11eb-91fe-0242ac110003\",\n    \"meta\": {\n        \"limit\": 3,\n        \"next\": \"/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/?limit=3&offset=3&type=local&country_iso=US\",\n        \"offset\": 0,\n        \"previous\": null,\n        \"total_count\": 14227\n    },\n    \"objects\": [\n        {\n            \"city\": \"Saddleback Valley\",\n            \"compliance_requirement\": {\n                \"business\": null,\n                \"individual\": null\n            },\n            \"country\": \"UNITED STATES\",\n            \"lata\": 730,\n            \"mms_enabled\": true,\n            \"mms_rate\": \"0.00400\",\n            \"monthly_rental_rate\": \"0.18000\",\n            \"number\": \"19496031645\",\n            \"prefix\": \"949\",\n            \"prerequisites\": [],\n            \"rate_center\": \"Saddleback Valley\",\n            \"region\": \"California\",\n            \"resource_uri\": \"/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/19496031645/\",\n            \"restriction\": \"\",\n            \"restriction_text\": \"\",\n            \"setup_rate\": \"0.00000\",\n            \"sms_enabled\": true,\n            \"sms_rate\": \"0.00000\",\n            \"sub_type\": \"local\",\n            \"type\": \"fixed\",\n            \"voice_enabled\": true,\n            \"voice_rate\": \"0.00300\"\n        },\n        {\n            \"city\": \"Elwin\",\n            \"compliance_requirement\": {\n                \"business\": null,\n                \"individual\": null\n            },\n            \"country\": \"UNITED STATES\",\n            \"lata\": 374,\n            \"mms_enabled\": true,\n            \"mms_rate\": \"0.00400\",\n            \"monthly_rental_rate\": \"0.18000\",\n            \"number\": \"12177674383\",\n            \"prefix\": \"217\",\n            \"prerequisites\": [],\n            \"rate_center\": \"Elwin\",\n            \"region\": \"Illinois\",\n            \"resource_uri\": \"/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/12177674383/\",\n            \"restriction\": \"\",\n            \"restriction_text\": \"\",\n            \"setup_rate\": \"0.00000\",\n            \"sms_enabled\": true,\n            \"sms_rate\": \"0.00000\",\n            \"sub_type\": \"local\",\n            \"type\": \"fixed\",\n            \"voice_enabled\": true,\n            \"voice_rate\": \"0.00300\"\n        },\n        {\n            \"city\": \"Westwood\",\n            \"compliance_requirement\": {\n                \"business\": null,\n                \"individual\": null\n            },\n            \"country\": \"UNITED STATES\",\n            \"lata\": 224,\n            \"mms_enabled\": true,\n            \"mms_rate\": \"0.00400\",\n            \"monthly_rental_rate\": \"0.18000\",\n            \"number\": \"15513105271\",\n            \"prefix\": \"551\",\n            \"prerequisites\": [],\n            \"rate_center\": \"Westwood\",\n            \"region\": \"New Jersey\",\n            \"resource_uri\": \"/v1/Account/MAMDC1NTE3ZGQ4NWNJNM/PhoneNumber/15513105271/\",\n            \"restriction\": \"\",\n            \"restriction_text\": \"\",\n            \"setup_rate\": \"0.00000\",\n            \"sms_enabled\": true,\n            \"sms_rate\": \"0.00000\",\n            \"sub_type\": \"local\",\n            \"type\": \"fixed\",\n            \"voice_enabled\": true,\n            \"voice_rate\": \"0.00300\"\n        }\n    ]\n}"
    }
  ]
}