Skip to main content

Logto Python SDK

Logto Stable Version PyPI - Python Version PyPI - License Discord

Prerequisites

  • Python 3.8 or higher
  • A Logto Cloud account or a self-hosted Logto
  • A Logto traditional web application created

If you don't have the Logto application created, please follow the ⚡ Get started guide to create one.

Installation

pip install logto # or `poetry add logto` or whatever you use

Tutorial

See tutorial for a quick start.

API reference

See API reference for more details.

Run the sample

There's a Flask sample in the samples directory. The sample has been tested with Python 3.8.17.

Install dependencies

This repo uses PDM as the package manager. To install the dependencies, run the following command in the root directory of the repo (not in the samples directory):

pdm install

Configure environment variables

To run the sample, you need to set the following environment variables:

APP_SECRET_KEY=your-secret-key # This is for Flask
LOGTO_ENDPOINT=http://your-logto-endpoint.com
LOGTO_APP_ID=your-logto-app-id
LOGTO_APP_SECRET=your-logto-app-secret
LOGTO_REDIRECT_URI=http://127.0.0.1:5000/sign-in-callback
LOGTO_POST_LOGOUT_REDIRECT_URI=http://127.0.0.1:5000/

Replace the values with your own.

For LOGTO_REDIRECT_URI and LOGTO_POST_LOGOUT_REDIRECT_URI, you should:

  1. Go to your Logto Console and add the URIs to the application's settings accordingly.
  2. Update the domain and port to match your local environment if necessary.

[!Note] The sample project also support dotenv. You can create a .env file in the root directory of the sample project and add the environment variables there.

Run the sample

In the root directory of the repo, run the following command:

pdm run flask

The script can be found in the pyproject.toml file.

Fetch user information

Call client.getIdTokenClaims() to get the basic user info. For a more detailed user info, you can call client.fetchUserInfo().

For details on fetching user info, see the Get user information.

Route protection

You have many ways to accomplish this.

Directly check the user's authentication status

You can call client.isAuthenticated() to check if the user is authenticated and can proceed with the request.

Use a decorator

You can create a decorator like @authenticated() to protect your routes. A sample decorator can be found at samples/authenticated.py.

For instance, an API may throw a 401 error if the user is not authenticated:

from flask import g, jsonify

@app.route("/api/protected")
@authenticated()
def protected():
    print(g.user) # The `@authenticated()` decorator sets the user object in the `g` object
    return jsonify({"message": "This is a protected route"})

Or, you can redirect the user to the sign-in page:

from flask import g, jsonify

@app.route("/protected")
@authenticated(shouldRedirect=True)
def protected():
    return "This is a protected route"

See the flask.py file for more details.

Resources

Metadata

Release files for logto 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logto 0.2.1
File Size Uploaded
logto-0.2.1.tar.gz 17.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logto 0.2.1
File Interpreter ABI Platform
logto-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 37.8 kB

Release files / logto-0.2.1.tar.gz

Download URL logto-0.2.1.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
33b2cc69fa254b5a01f31c1bc5f7bcf953f96d7259fa7b597f7dfc1f213c6f04
BLAKE2b-256 checksum
How to use checksums
90ac03b8ee35bea23c54f8ab09b17288ea94d7ad830905ee94e95c6f8bafe49a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.14.0 CPython/3.12.3 Darwin/23.4.0

Release files / logto-0.2.1-py3-none-any.whl

Download URL logto-0.2.1-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04288b9c9444464e32f838226b9220fda9e212de96589d5eb2d27d969cb6be7f
BLAKE2b-256 checksum
How to use checksums
c54143460f2622b998ed89c076dcee506bbb6fb0420cf81c199d8ad97ff5ed10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.14.0 CPython/3.12.3 Darwin/23.4.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page