Skip to content

Swift library for creating access tokens for Twilio SDKs

Notifications You must be signed in to change notification settings

brentschooley/TwilioAccessToken

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TwilioAccessToken

Build Status Platforms Swift Package Manager

Swift library for creating access tokens for Twilio SDKs from servers written in Swift.

⚠️ DISCLAIMER: Please do not use this directly in your iOS apps to generate tokens. Storing your Twilio credentials client-side is never a good idea. This library is intended for use with server-side Swift web frameworks like Vapor or Kitura. Read more about how to do this in this blog post.

Installation

Swift Package Manager

.Package(url: "https://github.com/brentschooley/TwilioAccessToken.git", majorVersion: 0, minor: 5)

Usage

You'll need a Twilio account so sign-up for a free trial first.

Set up your Twilio credentials in environment variables. You'll need at least the following:

  • Twilio Account Sid (stored as TWILIO_ACCOUNT_SID) - find it in the Console
  • Create an API Key and store the TWILIO_SIGNING_KEY and TWILIO_SECRET for this key

Start by creating an access token and setting an identity (this can be a username or whatever):

var accessToken = TwilioAccessToken(
  accountSid: accountSid,
  signingKeySid: signingKeySid,
  secret: secret
)

accessToken.identity = identity

Next, you'll want to create a grant or multiple grants that provide access to Twilio SDKs. This library supports generating tokens for Programmable Video, Programmable Chat, and Programmable Voice.

To generate a video grant, use this code:

var videoGrant = VideoGrant()
videoGrant.configurationProfileSid = "VS4xxxxxx" // your video configuration profile sid here

To generate a chat grant, use this code:

var chatGrant = IpMessagingGrant()
chatGrant.serviceSid = "IPMSxxxxxxxx" // your chat service sid here
chatGrant.endpointId = "NameOfApp:identity:device"

To generate a voice grant, use this code:

var voiceGrant = VoiceGrant()
voiceGrant.outgoingApplicationSid = "APxxxxxx" // your TwiML app sid here
voiceGrant.outgoingApplicationParams = ["To": "+12155551234", "From": "+14155551234"]
voiceGrant.endpointId = "NameOfApp:identity:device"

Once you've created your grant(s) you can add them to the token:

accessToken.addGrant(videoGrant)

Finally, you can return the JWT representation of the token:

accessToken.toJWT()

To read about how to use this in a Vapor app, read this blog post.

💝 Contributing

Please create an issue with a description of your problem or open a pull request with a fix.

✌️ License

MIT