SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

Authentication Errors with Twilio Functions After Rotating Auth Token

Issue/Error

After promoting a secondary Auth Token to primary, Twilio Functions integrations experience intermittent 401 Unauthorized authentication errors or degraded performance for up to 5 minutes.

Specifically, you may notice:

  • Outbound Twilio API calls made from a Function (using context.getTwilioClient() or direct HTTP requests with context.AUTH_TOKEN) are rejected due to invalid credentials.

  • Incoming requests to public Flex Functions using TokenValidator fail validation against /v1/Accounts/{AccountSid}/Tokens/validate.

  • Studio Flows or Flex tasks triggered by Functions fail and fall back to error-handling paths immediately following token rotation.

Product

Twilio Functions (Serverless)

Cause

When "Add my Twilio Credentials (ACCOUNT_SID) and (AUTH_TOKEN) to ENV" is enabled in your Twilio Functions Service settings, the Serverless runtime injects your credentials into the context object.

To maintain low latency and high performance, Twilio Functions caches the context object—including context.AUTH_TOKEN—for up to 5 minutes. 

When you promote a secondary Auth Token to primary:

  1. The previous primary Auth Token is immediately invalidated across Twilio's core APIs.

  2. Active, warm Function execution containers continue using the cached context.AUTH_TOKEN for up to 5 minutes until their local cache expires and refreshes with the newly promoted token.

Resolution

  1. Schedule token rotation during low-traffic windows: Because cached Function environments take up to 5 minutes to refresh, promote your secondary Auth Token during off-peak hours or scheduled maintenance windows whenever possible.

  2. Wait for the 5-minute cache window to clear: If you rotate your token during active traffic, intermittent authentication errors will resolve automatically within 5 minutes as warm containers expire and new instances initialize with the updated context.AUTH_TOKEN.

  3. Redeploy your Functions Service (optional): If you need to clear the cached credentials immediately after promoting your secondary Auth Token, trigger a new deployment of your Twilio Functions Service. Redeploying spins up fresh execution containers that pull the newly promoted token right away.

Additional Information

Have more questions? Submit a request
Powered by Zendesk