The Stytch Python library makes it easy to use the Stytch user infrastructure API in Python applications.
It pairs well with the Stytch Web SDK or your own custom authentication flow.
The Stytch Python library supports Python 3.7+
pip install stytch
You can find your API credentials in the Stytch Dashboard.
This client library supports all of Stytch's live products:
- Email Magic Links
- Embeddable Magic Links
- OAuth logins
- SMS passcodes
- WhatsApp passcodes
- Email passcodes
- Session Management
- WebAuthn
- Time-based one-time passcodes (TOTPs)
- Crypto wallets
- Passwords
Create an API client:
import stytch
client = stytch.Client(
project_id="project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
secret="secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
environment="test",
)Send a magic link by email:
login_or_create_resp = client.magic_links.email.login_or_create(
email="[email protected]",
login_magic_link_url="https://example.com/authenticate",
signup_magic_link_url="https://example.com/authenticate",
)
# Responses are fully-typed `pydantic` objects
print(login_or_create_resp)Authenticate the token from the magic link:
auth_resp = client.magic_links.authenticate(
token="DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=",
)
print(auth_resp)Every endpoint supports an async version which you can use by appending _async to the method name. You can use the
same Client object for sync and async methods. The above example of sending and authenticating an magic link can
be rewritten as:
login_or_create_resp = await client.magic_links.email.login_or_create_async(
email="[email protected]",
login_magic_link_url="https://example.com/authenticate",
signup_magic_link_url="https://example.com/authenticate",
)
print(login_or_create_resp)
auth_resp = await client.magic_links.authenticate(
token="DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=",
)
print(resp)Structured errors from the Stytch API will raise StytchError exceptions. You can view the details of the error through
the .details property of the StytchError exception.
from stytch.core.models import StytchError
try:
auth_resp = await client.magic_links.authenticate_async(token="token")
except StytchError as error:
# Handle Stytch errors here
if error.details.error_type == "invalid_token":
print("Whoops! Try again?")
except Exception as error:
# Handle other errors here
passLearn more about errors in the docs.
See example requests and responses for all the endpoints in the Stytch API Reference.
Follow one of the integration guides or start with one of our example apps.
If you've found a bug, open an issue!
If you have questions or want help troubleshooting, join us in Slack or email [email protected].
If you've found a security vulnerability, please follow our responsible disclosure instructions.
See DEVELOPMENT.md
Everyone interacting in the Stytch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.