Chilkat Online Tools

unicodeC / Twilio Messaging / SMS / List all messages sent from your Twilio number

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkDtObjW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW queryParams;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    HCkDtObjW date_created;
    HCkDtObjW date_sent;
    HCkDtObjW date_updated;
    const wchar_t *account_sid;
    const wchar_t *api_version;
    const wchar_t *body;
    const wchar_t *direction;
    int error_code;
    const wchar_t *error_message;
    const wchar_t *from;
    const wchar_t *messaging_service_sid;
    const wchar_t *num_media;
    const wchar_t *num_segments;
    const wchar_t *price;
    const wchar_t *price_unit;
    const wchar_t *sid;
    const wchar_t *status;
    const wchar_t *v_to;
    int v_end;
    const wchar_t *first_page_uri;
    const wchar_t *next_page_uri;
    int page;
    int page_size;
    const wchar_t *previous_page_uri;
    int start;
    const wchar_t *uri;
    int i;
    int count_i;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    CkHttpW_putBasicAuth(http,TRUE);
    CkHttpW_putLogin(http,L"{{TWILIO_ACCOUNT_SID}}");
    CkHttpW_putPassword(http,L"{{TWILIO_AUTH_TOKEN}}");

    queryParams = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(queryParams,L"From",L"{{TWILIO_PHONE_NUMBER}}");

    resp = CkHttpW_QuickRequestParams(http,L"GET",L"https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json",queryParams);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(queryParams);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(queryParams);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "end": 51142961,
    //   "first_page_uri": "https://ZvltuXKxENEyvG.fnbzDIxrccIwHE6GxsyHjMd",
    //   "messages": [
    //     {
    //       "account_sid": "AC9E",
    //       "api_version": "commodo sint sunt eu",
    //       "body": "ut cillum",
    //       "date_created": "fugiat ipsum enim quis",
    //       "date_sent": "o",
    //       "date_updated": "laborum id esse elit",
    //       "direction": "outbound-reply",
    //       "error_code": 89665305,
    //       "error_message": "deserunt",
    //       "from": "anim esse",
    //       "messaging_service_sid": "MGC0",
    //       "num_media": "enim et",
    //       "num_segments": "quis qui ipsum",
    //       "price": "exercitation Duis",
    //       "price_unit": "consequat qu",
    //       "sid": "SMe5",
    //       "status": "scheduled",
    //       "subresource_uris": {},
    //       "to": "nulla",
    //       "uri": "cillum ut in irure"
    //     },
    //     {
    //       "account_sid": "ACD4",
    //       "api_version": "incididunt in non",
    //       "body": "deserunt incididunt irure eiusmod",
    //       "date_created": "sint ea",
    //       "date_sent": "Ut ut consectetur",
    //       "date_updated": "non dolore",
    //       "direction": "outbound-api",
    //       "error_code": -69524035,
    //       "error_message": "dolore Ut officia",
    //       "from": "reprehenderit",
    //       "messaging_service_sid": "MGEe",
    //       "num_media": "voluptate fugiat eiusmod qui Ut",
    //       "num_segments": "ea",
    //       "price": "consequat",
    //       "price_unit": "fugiat aliquip",
    //       "sid": "MM38",
    //       "status": "received",
    //       "subresource_uris": {},
    //       "to": "in tempor labore eiusmod",
    //       "uri": "elit non dol"
    //     }
    //   ],
    //   "next_page_uri": "http://fSikGpthYZQbOt.sncsPW4TzW44TkKnuDgx8f2FJz.",
    //   "page": -43897926,
    //   "page_size": 12121047,
    //   "previous_page_uri": "https://lSrGpSrECCMzKudcyRlwJdfhlnZEaAQ.hytgI,rCvOIy,x1C6vbgTvc9p6X2hkAdrqkd5jx8JKwJPiCrpsg2w",
    //   "start": -23084704,
    //   "uri": "https://USSzIQwA.hvxpmaT2476DlQGIi"
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    date_created = CkDtObjW_Create();
    date_sent = CkDtObjW_Create();
    date_updated = CkDtObjW_Create();

    v_end = CkJsonObjectW_IntOf(jResp,L"end");
    first_page_uri = CkJsonObjectW_stringOf(jResp,L"first_page_uri");
    next_page_uri = CkJsonObjectW_stringOf(jResp,L"next_page_uri");
    page = CkJsonObjectW_IntOf(jResp,L"page");
    page_size = CkJsonObjectW_IntOf(jResp,L"page_size");
    previous_page_uri = CkJsonObjectW_stringOf(jResp,L"previous_page_uri");
    start = CkJsonObjectW_IntOf(jResp,L"start");
    uri = CkJsonObjectW_stringOf(jResp,L"uri");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"messages");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        account_sid = CkJsonObjectW_stringOf(jResp,L"messages[i].account_sid");
        api_version = CkJsonObjectW_stringOf(jResp,L"messages[i].api_version");
        body = CkJsonObjectW_stringOf(jResp,L"messages[i].body");
        CkJsonObjectW_DtOf(jResp,L"messages[i].date_created",FALSE,date_created);
        CkJsonObjectW_DtOf(jResp,L"messages[i].date_sent",FALSE,date_sent);
        CkJsonObjectW_DtOf(jResp,L"messages[i].date_updated",FALSE,date_updated);
        direction = CkJsonObjectW_stringOf(jResp,L"messages[i].direction");
        error_code = CkJsonObjectW_IntOf(jResp,L"messages[i].error_code");
        error_message = CkJsonObjectW_stringOf(jResp,L"messages[i].error_message");
        from = CkJsonObjectW_stringOf(jResp,L"messages[i].from");
        messaging_service_sid = CkJsonObjectW_stringOf(jResp,L"messages[i].messaging_service_sid");
        num_media = CkJsonObjectW_stringOf(jResp,L"messages[i].num_media");
        num_segments = CkJsonObjectW_stringOf(jResp,L"messages[i].num_segments");
        price = CkJsonObjectW_stringOf(jResp,L"messages[i].price");
        price_unit = CkJsonObjectW_stringOf(jResp,L"messages[i].price_unit");
        sid = CkJsonObjectW_stringOf(jResp,L"messages[i].sid");
        status = CkJsonObjectW_stringOf(jResp,L"messages[i].status");
        v_to = CkJsonObjectW_stringOf(jResp,L"messages[i].to");
        uri = CkJsonObjectW_stringOf(jResp,L"messages[i].uri");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(queryParams);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);
    CkDtObjW_Dispose(date_created);
    CkDtObjW_Dispose(date_sent);
    CkDtObjW_Dispose(date_updated);

    }

Curl Command

curl -G -d "From=%7B%7BTWILIO_PHONE_NUMBER%7D%7D"
	-u '{{TWILIO_ACCOUNT_SID}}:{{TWILIO_AUTH_TOKEN}}'
https://api.twilio.com/2010-04-01/Accounts/:AccountSid/Messages.json

Postman Collection Item JSON

{
  "name": "List all messages sent from your Twilio number",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var template = `",
          "    <table bgcolor=\"#FFFFFF\">",
          "        <tr>",
          "            <th>To</th>",
          "            <th>Message Body</th>",
          "            <th>Sent at</th>",
          "        </tr>",
          "",
          "        {{#each messages}}",
          "            <tr>",
          "                <td>{{to}}</td>",
          "                <td>{{body}}</td>",
          "                <td>{{date_sent}}</td>",
          "            </tr>",
          "        {{/each}}",
          "    </table>",
          "`;",
          "",
          "// Set visualizer",
          "pm.visualizer.set(template, {",
          "    // Pass the response body parsed as JSON as `data`",
          "    messages: pm.response.json().messages",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "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-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json?From={{TWILIO_PHONE_NUMBER}}",
      "host": [
        "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
      ],
      "path": [
        "2010-04-01",
        "Accounts",
        ":AccountSid",
        "Messages.json"
      ],
      "query": [
        {
          "key": "To",
          "value": "<string>",
          "description": "Read messages sent to only this phone number.",
          "disabled": true
        },
        {
          "key": "From",
          "value": "{{TWILIO_PHONE_NUMBER}}",
          "description": "Read messages sent from only this phone number or alphanumeric sender ID."
        },
        {
          "key": "DateSent",
          "value": "<dateTime>",
          "description": "The date of the messages to show. Specify a date as `YYYY-MM-DD` in GMT to read only messages sent on this date. For example: `2009-07-06`. You can also specify an inequality, such as `DateSent<=YYYY-MM-DD`, to read messages sent on or before midnight on a date, and `DateSent>=YYYY-MM-DD` to read messages sent on or after midnight on a date.",
          "disabled": true
        },
        {
          "key": "DateSent<",
          "value": "<dateTime>",
          "description": "The date of the messages to show. Specify a date as `YYYY-MM-DD` in GMT to read only messages sent on this date. For example: `2009-07-06`. You can also specify an inequality, such as `DateSent<=YYYY-MM-DD`, to read messages sent on or before midnight on a date, and `DateSent>=YYYY-MM-DD` to read messages sent on or after midnight on a date.",
          "disabled": true
        },
        {
          "key": "DateSent>",
          "value": "<dateTime>",
          "description": "The date of the messages to show. Specify a date as `YYYY-MM-DD` in GMT to read only messages sent on this date. For example: `2009-07-06`. You can also specify an inequality, such as `DateSent<=YYYY-MM-DD`, to read messages sent on or before midnight on a date, and `DateSent>=YYYY-MM-DD` to read messages sent on or after midnight on a date.",
          "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) that created the Message resources to read."
        }
      ]
    },
    "description": "Retrieve a list of messages belonging to the account used to make the request. You can filter for example for only the messages that were sent from a specific phone number.\n\nRead more about it in the [Twilio Docs](https://www.twilio.com/docs/sms/api/message-resource#read-multiple-message-resources)"
  },
  "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-Messages.json-Url}}/2010-04-01/Accounts/:AccountSid/Messages.json?To=<string>&From=<string>&DateSent=<dateTime>&DateSent<=<dateTime>&DateSent>=<dateTime>&PageSize=<integer>",
          "host": [
            "{{2010-04-01-Accounts-AccountSid-Messages.json-Url}}"
          ],
          "path": [
            "2010-04-01",
            "Accounts",
            ":AccountSid",
            "Messages.json"
          ],
          "query": [
            {
              "key": "To",
              "value": "<string>"
            },
            {
              "key": "From",
              "value": "<string>"
            },
            {
              "key": "DateSent",
              "value": "<dateTime>"
            },
            {
              "key": "DateSent<",
              "value": "<dateTime>"
            },
            {
              "key": "DateSent>",
              "value": "<dateTime>"
            },
            {
              "key": "PageSize",
              "value": "<integer>"
            }
          ],
          "variable": [
            {
              "key": "AccountSid"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"end\": 51142961,\n \"first_page_uri\": \"https://ZvltuXKxENEyvG.fnbzDIxrccIwHE6GxsyHjMd\",\n \"messages\": [\n  {\n   \"account_sid\": \"AC9E\",\n   \"api_version\": \"commodo sint sunt eu\",\n   \"body\": \"ut cillum\",\n   \"date_created\": \"fugiat ipsum enim quis\",\n   \"date_sent\": \"o\",\n   \"date_updated\": \"laborum id esse elit\",\n   \"direction\": \"outbound-reply\",\n   \"error_code\": 89665305,\n   \"error_message\": \"deserunt\",\n   \"from\": \"anim esse\",\n   \"messaging_service_sid\": \"MGC0\",\n   \"num_media\": \"enim et\",\n   \"num_segments\": \"quis qui ipsum\",\n   \"price\": \"exercitation Duis\",\n   \"price_unit\": \"consequat qu\",\n   \"sid\": \"SMe5\",\n   \"status\": \"scheduled\",\n   \"subresource_uris\": {},\n   \"to\": \"nulla\",\n   \"uri\": \"cillum ut in irure\"\n  },\n  {\n   \"account_sid\": \"ACD4\",\n   \"api_version\": \"incididunt in non\",\n   \"body\": \"deserunt incididunt irure eiusmod\",\n   \"date_created\": \"sint ea\",\n   \"date_sent\": \"Ut ut consectetur\",\n   \"date_updated\": \"non dolore\",\n   \"direction\": \"outbound-api\",\n   \"error_code\": -69524035,\n   \"error_message\": \"dolore Ut officia\",\n   \"from\": \"reprehenderit\",\n   \"messaging_service_sid\": \"MGEe\",\n   \"num_media\": \"voluptate fugiat eiusmod qui Ut\",\n   \"num_segments\": \"ea\",\n   \"price\": \"consequat\",\n   \"price_unit\": \"fugiat aliquip\",\n   \"sid\": \"MM38\",\n   \"status\": \"received\",\n   \"subresource_uris\": {},\n   \"to\": \"in tempor labore eiusmod\",\n   \"uri\": \"elit non dol\"\n  }\n ],\n \"next_page_uri\": \"http://fSikGpthYZQbOt.sncsPW4TzW44TkKnuDgx8f2FJz.\",\n \"page\": -43897926,\n \"page_size\": 12121047,\n \"previous_page_uri\": \"https://lSrGpSrECCMzKudcyRlwJdfhlnZEaAQ.hytgI,rCvOIy,x1C6vbgTvc9p6X2hkAdrqkd5jx8JKwJPiCrpsg2w\",\n \"start\": -23084704,\n \"uri\": \"https://USSzIQwA.hvxpmaT2476DlQGIi\"\n}"
    }
  ]
}