Transport for the Twilio APIs: HTTP Basic credentials, form encoding, and page links.
Per-call credentials. Every API-calling function in this package
takes credentials three ways, all resolved by resolve: a Client as
its first argument, an optional trailing opts Map, or context
variables. Nothing from opts is written to ctx, so one run can act
for many accounts and parallel flows don't share credentials. Secrets
in a Client or in opts are masked in call records.
account-sid: the account for this call. It needs credentials in the sameopts: context credentials are never sent to a per-call account.auth-token: the account's Auth Token. It needsaccount-sid.api-key-sid,api-key-secret: an API key. They needaccount-sid, and win overauth-token.
With credentials in opts, context variables are ignored for that
call. One of those keys present but blank or null is an error, never a
fallback to context.
Context Vars
twilio.account.sidtwilio.auth.tokentwilio.api.key.sidtwilio.api.key.secret
Values
CREDENTIAL_KEYS
CREDENTIAL_KEYS: Vec [
"account-sid",
"auth-token",
"api-key-sid",
"api-key-secret"
]
Functions
account-url
fn (client: Client): Str
The 2010-04-01 base URL scoped to the client's account:
https://api.twilio.com/2010-04-01/Accounts/{AccountSid}.
Example
::api ::twilio::api
c ::api/Client({account-sid: "AC123", username: "AC123", password: "test"})
::api/account-url(c) // "https://api.twilio.com/2010-04-01/Accounts/AC123"
api-key-client
fn (account-sid: Str, key-sid: Str, key-secret: Str): Client
A Client that authenticates with an API Key (SK... SID plus secret)
against the account account-sid. Twilio recommends API keys over the
Auth Token for production: they can be revoked individually and never
grant access to the account's own credentials.
Example
c ::twilio::api/api-key-client("AC1234...", "SK5678...", "key-secret")
::twilio::accounts/get-account(c)
auth-header
fn (client: Client): Str
The HTTP Basic Authorization header value for a Client. Exposed for
testing.
Example
::api ::twilio::api
c ::api/Client({account-sid: "AC123", username: "AC123", password: "test"})
::api/auth-header(c) // "Basic QUMxMjM6dGVzdA=="
auth-token-client
fn (account-sid: Str, auth-token: Str): Client
A Client that authenticates with an Account SID and its Auth Token.
Example
sub ::twilio::api/auth-token-client("AC1111...", "subaccount-auth-token")
::twilio::messages/list-messages(sub, ::twilio::messages/ListMessagesRequest({PageSize: 20}))
blank-credential
fn (opts: Map, names: Vec): Str?
The first of names that opts has but with a blank or null value,
else null. Such a key is an error, never a fallback to context: a tenant
whose key is missing must not run on the project's credentials.
Example
::api ::twilio::api
::api/blank-credential({auth-token: ""}, ["account-sid", "auth-token"]) // "auth-token"
::api/blank-credential({}, ["auth-token"]) // null
client-from-ctx
fn (): Client
The Client described by context variables: twilio.account.sid plus
either an API key (twilio.api.key.sid and twilio.api.key.secret,
preferred when both are set) or twilio.auth.token. An Err with a clear
message when the Account SID or both credential forms are missing. Same
as resolve({}).
Example
::api ::twilio::api
c ::api/client-from-ctx()
::twilio::accounts/get-account(c).friendly_name
twilio.account.sid, twilio.auth.token, twilio.api.key.sid, twilio.api.key.secret
client-from-values
fn (account-sid: Str, key-sid: Str?, key-secret: Str?, auth-token: Str?, source: Str): Client
A Client from an Account SID and the credentials given, API key first,
or an Err when an API key is half set or there is no credential.
source names where the values came from, for the error.
Example
::api ::twilio::api
::api/client-from-values("AC123", null, null, "test-token", "the test").username // "AC123"
is-err(::api/client-from-values("AC123", null, null, null, "the test")) // true
collect-pages
fn (page: Map, key: Str, limit: Int, opts: Map): Vec<Map>
fn (client: Client, page: Map, key: Str, limit: Int): Vec<Map>
fn (client: Client, page: Map, key: Str, limit: Int, acc: Vec): Vec<Map>
Collect the items under key from page and the pages after it, up
to limit items (pages are fetched only while more are needed). Each
list-all-* function uses this. A failed page comes back as its Err.
Example
::api ::twilio::api
c ::api/client-from-ctx()
page ::api/json(c, "GET", `${::api/account-url(c)}/Calls.json?PageSize=100`)
calls ::api/collect-pages(c, page, "calls", 250)
length(calls)
delete-request
fn (url: Str, opts: Map): Bool
fn (client: Client, url: Str): Bool
DELETE a resource; true on success (Twilio answers 204),
err(HttpError) otherwise.
Example
::api ::twilio::api
c ::api/client-from-ctx()
deleted ::api/delete-request(c, `${::api/account-url(c)}/Messages/SM123.json`)
if(deleted, "Removed")
form-encode
fn (data: Map): Str
Encode a Map as application/x-www-form-urlencoded. Null values are
omitted. Vec values are encoded as repeated parameters
(MediaUrl=a&MediaUrl=b) per Twilio's convention.
Example
::api ::twilio::api
::api/form-encode({To: "+15558675310", Body: null}) // "To=%2B15558675310"
::api/form-encode({MediaUrl: ["a.png", "b.png"]}) // "MediaUrl=a.png&MediaUrl=b.png"
is-twilio-url
fn (url: Str): Bool
True for an https:// URL on a twilio.com host. Pagination only
follows these, so a page reference never sends credentials elsewhere.
Exposed for testing.
Example
::api ::twilio::api
::api/is-twilio-url("https://verify.twilio.com/v2/Services") // true
::api/is-twilio-url("https://twilio.com.example.org/v2") // false
json
fn (client: Client, method: Str, url: Str): Map
fn (method: Str, url: Str, body: Map, opts: Map): Map
fn (client: Client, method: Str, url: Str, body: Map): Map
Make a request and return the parsed JSON body, or err(HttpError) on a
non-2xx response.
Example
::api ::twilio::api
c ::api/client-from-ctx()
body ::api/json(c, "GET", `${::api/account-url(c)}/Messages.json?PageSize=5`)
map(body.messages, %.status)
next-page
fn (page: Map): Map?
fn (page: Map, opts: Map): Map?
fn (client: Client, page: Map): Map?
Fetch the page after page (any list response). Returns the next
page's Map, or null when page is the last one.
Example
page1 ::twilio::messages/list-messages(::twilio::messages/ListMessagesRequest({PageSize: 50}))
page2 ::twilio::api/next-page(page1)
next-page-ref
fn (page: Map): Str?
The next-page reference of a list response, from next_page_uri
(2010-04-01 APIs) or meta.next_page_url (v2 APIs); null on the last
page. Exposed for testing.
Example
::api ::twilio::api
::api/next-page-ref({next_page_uri: "/2010-04-01/Accounts/AC123/Messages.json?PageToken=PA1"}) // "/2010-04-01/Accounts/AC123/Messages.json?PageToken=PA1"
::api/next-page-ref({meta: {next_page_url: "https://verify.twilio.com/v2/Services?Page=1"}}) // "https://verify.twilio.com/v2/Services?Page=1"
::api/next-page-ref({messages: []}) // null
page-url
fn (ref: Str): Str
Resolve a page reference to an absolute URL: a path is joined to
https://api.twilio.com; an absolute URL must be on a twilio.com host.
Exposed for testing.
Example
::api ::twilio::api
::api/page-url("/2010-04-01/Accounts/AC123/Calls.json?Page=1") // "https://api.twilio.com/2010-04-01/Accounts/AC123/Calls.json?Page=1"
is-err(::api/page-url("https://example.com/next")) // true
query-string
fn (params: Map): Str
Build a query string (?a=1&b=2) from a Map. Null values are omitted;
returns an empty string when no parameters remain. Values are
percent-encoded, so + in phone numbers is preserved.
Example
::api ::twilio::api
::api/query-string({PageSize: 20, Page: null}) // "?PageSize=20"
::api/query-string({Page: null}) // ""
request
fn (method: Str, url: Str): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Map): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Map, opts: Map): ::hot::http/HttpResponse
fn (client: Client, method: Str, url: Str, additional-headers: Map, body: Map): ::hot::http/HttpResponse
fn (method: Str, url: Str, additional-headers: Map, body: Map, account-sid: Str, auth-token: Str): ::hot::http/HttpResponse
Make an authenticated HTTP request to a Twilio API. Uses HTTP Basic Auth
with the credentials of a Client (the Client-first form), of per-call
opts (the trailing Map), or of the context (the shorter forms). POST
bodies are form-encoded (application/x-www-form-urlencoded) per Twilio's
API convention; null values are dropped and Vec values become repeated
parameters.
The 6-arity form takes an Account SID and Auth Token directly.
Example
::api ::twilio::api
// Simple GET request with context credentials
response ::api/request("GET", `${::api/account-url(::api/client-from-ctx())}/Messages.json`)
// POST with form-encoded body and an explicit client
c ::api/api-key-client("AC123", "SK456", "secret")
response ::api/request(c, "POST", `${::api/account-url(c)}/Messages.json`, {}, {To: "+15558675310", From: "+15017122661", Body: "Hello"})
twilio.account.sid, twilio.auth.token, twilio.api.key.sid, twilio.api.key.secret
resolve
fn (opts: Map): Client
fn (client: Client): Client
The Client for one call: the one place credentials are decided. A
Client passes through; opts go through resolve-from with the
context. Either way the secret is masked in call records with
::hot::ctx/mark-secret, and opts values are never written to ctx.
Example
c ::twilio::api/resolve({account-sid: "AC1111...", auth-token: "tenant-auth-token"})
::twilio::api/account-url(c)
twilio.account.sid, twilio.auth.token, twilio.api.key.sid, twilio.api.key.secret
resolve-from
fn (opts: Map, ctx-values: Map): Client
The one resolver for credentials by value, pure: a Client for a call
from opts first, then ctx-values (a Map of context variables). An
Err when a credential key in opts is blank or null (that never falls
back to context), when opts has account-sid without credentials or
credentials without account-sid, when opts has half an API key, or
when nothing is set. Exposed for testing; resolve feeds it the context.
Example
::api ::twilio::api
ctx-values {"twilio.account.sid": "AC123", "twilio.auth.token": "test-token"}
::api/resolve-from({}, ctx-values).account-sid // "AC123"
is-err(::api/resolve-from({account-sid: "AC999"}, ctx-values)) // true
Types
Client
Client type {
account-sid: Str,
username: Str,
password: Str
}
The credentials one Twilio call is made with. account-sid (AC...) is
the account every resource URL is scoped to; username/password are
the HTTP Basic credentials: the Account SID and Auth Token, or an API
Key SID (SK...) and its secret. Build one with client-from-ctx,
auth-token-client, or api-key-client, and pass it as the first
argument of any resource function to call a different account (a
subaccount, a customer's account) than the one in context. The
constructors mask the secret in call records.
HttpError
HttpError type {
status: Int,
headers: Map,
body: Any
}