Please excuse our look. We're just getting started here.

Want to learn more about Twilio Forums? Check out our FAQ page here.

How to create an access token chat for conversation with golang?

thaibui
thaibui
edited January 13 in Twilio Community

I can not found rest api for create access token chat for conversation. I build backend my app with golang and need to gen access token chat for sending to client

Best Answer

  • sbrijmohan
    sbrijmohan admin
    edited January 13 Answer ✓

    Hi again @thaibui , the following links might provide you with some clarity.

    There’s an example on how to create Access tokens here: https://github.com/twilio/twilio-go#building-access-tokens However, the code snippet is built for Voice. In order to use it for conversations, they should import the Chat Grant and initialize it with the Conversation Service Sid.

    Here's a code snippet for your reference:

    accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
    	chatServiceSid := os.Getenv("TWILIO_CHAT_SERVICE_SID")
    	apiKey := os.Getenv("TWILIO_API_KEY")
    	apiSecret := os.Getenv("TWILIO_API_SECRET")
    	identity := "user_identity"
    
    	params := jwt.AccessTokenParams{
    		AccountSid:    accountSid,
    		SigningKeySid: apiKey,
    		Secret:        apiSecret,
    		Identity:      identity,
    	}
    
    	jwtToken := jwt.CreateAccessToken(params)
    	chatGrant := &jwt.ChatGrant{
    		ServiceSid: chatServiceSid,
    	}
    
    	jwtToken.AddGrant(chatGrant)
    	token, err := jwtToken.ToJwt()
    

    Also, Check out this link: https://www.twilio.com/docs/iam/access-tokens?code-sample=code-creating-an-access-token-conversations&code-language=Python&code-sdk-version=7.x where you can create short-lived Access Tokens to authenticate Twilio Client SDKs like Voice, Conversations, and Video.

    I hope this helps! ✨

Answers

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support