Skip to content

fix!: raise a clear error when an API Key SID is used without account_sid - #990

Open
saryeljaramany wants to merge 1 commit into
twilio:mainfrom
saryeljaramany:fix/api-key-without-account-sid
Open

saryeljaramany wants to merge 1 commit into
twilio:mainfrom
saryeljaramany:fix/api-key-without-account-sid

Conversation

@saryeljaramany

Copy link
Copy Markdown

Fixes

Fixes #985

When Client is created with an API Key SID (SK...) as the username and no account_sid, ClientBase.__init__ silently falls back to account_sid = username. The mistake only shows up later as an unrelated authentication / not-found error from the API.

This PR raises a TwilioException in the constructor when all of these are true:

  • account_sid is not provided
  • no credential_provider is provided
  • username (including a value read from TWILIO_ACCOUNT_SID) starts with SK

The message tells the user that an API Key SID requires the Account SID to be passed via account_sid. No AC prefix check is added, and all other setups are unchanged. This is the same hint twilio-node gives for this mistake.

Breaking change

Client("SK...", secret) previously constructed without error and now raises. Anyone relying on that to call products that do not use account-scoped paths would need to pass account_sid=. I'm happy to switch this to a warnings.warn if maintainers prefer a non-breaking approach.

Tests

New unit tests cover: SK without account_sid raises (also via the environment variable), SK with account_sid="AC..." works, and AC + auth token still works.

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clearer diagnostic when an API Key SID is passed without account_sid

1 participant