Skip to main content

Stytch python client

Project description

Stytch Python Library

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.

Requirements

The Stytch Python library supports Python 3.8+

Installation

pip install stytch

Usage

You can find your API credentials in the Stytch Dashboard.

This client library supports all Stytch's live products:

B2C

B2B

Example B2C usage

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=",
    # Optionally specify a custom base URL for all API calls
    # custom_base_url="https://api.custom-domain.com/",
)

Send a magic link by email:

login_or_create_resp = client.magic_links.email.login_or_create(
    email="sandbox@stytch.com",
    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)

Async support

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="sandbox@stytch.com",
    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)

Example B2B usage

Create an API client:

Python:

import stytch

client = stytch.B2BClient(
    project_id="project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
    secret="secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
    # Optionally specify a custom base URL for all API calls
    # custom_base_url="https://api.custom-domain.com/",
)

Create an organization

response = client.organizations.create(
    organization_name="Acme Co",
    organization_slug="acme-co",
    email_allowed_domains=["acme.co"]
)

Log the first user into the organization

response = client.magic_links.email.login_or_signup(
    organization_id="ORGANIZATION_ID_FROM_RESPONSE",
    email_address="admin@acme.co",
    login_redirect_url="https://example.com/authenticate",
    signup_redirect_url="https://example.com/authenticate"
)

Handling Errors

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.response_base 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
    pass

Learn more about errors in the docs.

Documentation

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.

Support

If you've found a bug, open an issue!

If you have questions or want help troubleshooting, join us in Slack or email support@stytch.com.

If you've found a security vulnerability, please follow our responsible disclosure instructions.

Development

See DEVELOPMENT.md

Code of Conduct

Everyone interacting in the Stytch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stytch-14.2.0.tar.gz (176.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stytch-14.2.0-py3-none-any.whl (304.3 kB view details)

Uploaded Python 3

File details

Details for the file stytch-14.2.0.tar.gz.

File metadata

  • Download URL: stytch-14.2.0.tar.gz
  • Upload date:
  • Size: 176.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stytch-14.2.0.tar.gz
Algorithm Hash digest
SHA256 1809d0e12c86cc1f3c4a65156030c69a4a33c53464093fe99095f11efcd2a2c7
MD5 ffa543d7a245ec95fc21e10a2d40aa86
BLAKE2b-256 6d1f897db69d280fde94ab3bf7c1359450675806ca7e72b8745ed42c60a64458

See more details on using hashes here.

File details

Details for the file stytch-14.2.0-py3-none-any.whl.

File metadata

  • Download URL: stytch-14.2.0-py3-none-any.whl
  • Upload date:
  • Size: 304.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stytch-14.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd0d5b91e75ebf6a3072cfc49319fc8db967c6c5ea05d67613b2d78ee6d1ca78
MD5 410c559d739e6182591ad0be23df2a5e
BLAKE2b-256 f325fb726f4c7006ad44f207e0f0f8eb1ba26f621b703a545ec65a510203fb53

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page