# `Twilio.AccessToken`

Access tokens for Twilio's client SDKs: the Voice SDK in a browser or app,
Video, Conversations (Chat), Sync.

Your server makes a short-lived token for a user, and the client SDK uses
it to talk to Twilio directly:

    token =
      Twilio.AccessToken.generate(
        identity: "alice",
        grants: [voice: [outgoing_application_sid: "AP...", incoming_allow: true]]
      )

Tokens are signed with an [API key](https://www.twilio.com/docs/iam/api-keys)
(`SK...` and its secret), not your auth token. The key, its secret and your
account SID come from the options, else from your config, else from the
environment:

    config :twilio,
      account_sid: "AC...",
      api_key_sid: "SK...",
      api_key_secret: "..."

    # or TWILIO_ACCOUNT_SID, TWILIO_API_KEY and TWILIO_API_SECRET

The token is the same JWT Twilio's official SDKs make: HS256, a
`twilio-fpa;v=1` content type, and the grants below.

# `generate`

```elixir
@spec generate(keyword()) :: String.t()
```

A signed access token, as a string.

Options:

  * `:identity`: who the token is for (required for Voice, Chat and Sync)
  * `:grants`: what the token allows, a keyword list:
      * `voice: [incoming_allow: true, outgoing_application_sid: "AP...",
        outgoing_application_params: %{...}, push_credential_sid: "CR...",
        endpoint_id: "..."]`
      * `video: [room: "..."]`
      * `chat: [service_sid: "IS...", endpoint_id: "...",
        deployment_role_sid: "...", push_credential_sid: "..."]`
        (Conversations uses this grant too)
      * `sync: [service_sid: "IS...", endpoint_id: "..."]`
      * `task_router: [workspace_sid: "WS...", worker_sid: "WK...", role: "..."]`
      * `playback: %{...}`, the grant Twilio Live's API returned
  * `:ttl`: seconds the token is valid (default 3600; Twilio allows up to 24 hours)
  * `:valid_until`: a Unix time when it expires, instead of `:ttl`
  * `:nbf`: a Unix time before which it isn't valid (default: now; `false` to leave it out)
  * `:region`: the Twilio region, such as `"ie1"`
  * `:account_sid`, `:api_key_sid`, `:api_key_secret`: see the module docs
  * `:now`: the current Unix time (for tests)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
