Swift library for creating access tokens for Twilio SDKs from servers written in Swift.
.Package(url: "https://github.com/brentschooley/TwilioAccessToken.git", majorVersion: 0, minor: 5)
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
andTWILIO_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.
Please create an issue with a description of your problem or open a pull request with a fix.
MIT