Python SDK
The Kinde Python SDK allows developers to quickly and securely integrate a new or an existing Python application into the Kinde platform.
Before you begin
Link to this section- Kinde Python SDK supports Python 3.8+
- If you haven’t already got a Kinde account, register for free here (no credit card required). Registering gives you a Kinde domain, which you need to get started, e.g.
yourapp.kinde.com
.
For new projects, you can also find our Starter Kit on GitHub.
Install
Link to this sectionInstall PIP and then execute the following command:
Set callback URLs
Link to this section- In Kinde, go to Settings > Applications > [Your app] > View details.
- Add your callback URLs in the relevant fields. For example:
- Allowed callback URLs (also known as redirect URIs) - for example,
http://localhost:8000/callback
- Allowed logout redirect URLs - for example,
http://localhost:8000
- Allowed callback URLs (also known as redirect URIs) - for example,
- Select Save.
Add environments
Link to this sectionKinde comes with a production environment, but you can set up other environments if you want to. Note that each environment needs to be set up independently, so you need to use the Environment subdomain in the code block above for those new environments.
Configure your app
Link to this sectionEnvironment variables
The following variables need to be replaced in the code snippets below.
KINDE_HOST
- your Kinde domain, e.g.https://yourdomain.kinde.com
KINDE_CLIENT_ID
- In Kinde, go to Settings > Applications > [your application] > View details.KINDE_CLIENT_SECRET
- In Kinde, go to Settings > Applications > [your application] > View details.KINDE_REDIRECT_URL
- your callback urls or redirect URIs, e.g.http://localhost:8000/callback
KINDE_POST_LOGOUT_REDIRECT_URL
- where you want users to be redirected to after signing out, e.g.http://localhost:8000
Integrate with your app
Link to this sectionCreate a new instance of the Kinde Auth client object before you initialize your app.
With PKCE flow, the code_verifier
is required.
Sign in and sign up
Link to this sectionThe Kinde client provides methods for easy sign in and sign up. You can add buttons in your HTML as follows:
You will also need to route /login
and /register
to the SDK methods:
Manage redirects
Link to this sectionWhen the user is redirected back to your site from Kinde, this will call your callback URL defined in the KINDE_REDIRECT_URL
variable. You will need to route /callback
to call a function to handle this.
You can also get the current authentication status with is_authenticated
.
Note: The kinde_client object that is created stores the access_token. This means you need to create a kinde_client object for each unique user that is signing in to your application, so that you can keep track of whether the user is authenticated or not.
Logout
Link to this sectionThe SDK comes with a logout method that returns a logout URL.
Get user information
Link to this sectionTo access the user information, use the get_user_details
helper function:
View users in Kinde
Link to this sectionGo to the Users page in Kinde to see who has registered.
User permissions
Link to this sectionAfter a user signs in and they are verified, the token return includes permissions for that user. User permissions are set in Kinde, but you must also configure your application to unlock these functions.
We provide helper functions to more easily access permissions:
A practical example in code might look something like:
Feature Flags
Link to this sectionWe have provided a helper to grab any feature flag from access_token
:
We also provide wrapper functions which should leverage getFlag
above.
Get boolean flags
Get string flags
Get integer flags
Audience
Link to this sectionAn audience
is the intended recipient of an access token - for example the API for your application. The audience argument can be passed to the Kinde client to request an audience be added to the provided token.
The audience of a token is the intended recipient of the token.
For details on how to connect, see Register an API
Overriding scope
Link to this sectionBy default the KindeSDK
requests the following scopes:
- profile
- offline
- openid
You can override this by passing scope into the KindeSDK
.
Getting claims
Link to this sectionWe have provided a helper to grab any claim from your id or access tokens. The helper defaults to access tokens:
Organizations
Link to this sectionCreate an organization
Link to this sectionTo create a new organization within your application, you will need to run a similar function to below:
Sign up and sign in to organizations
Link to this sectionKinde has a unique code for every organization. You’ll have to pass this code through when you register a new user or sign in to a particular organization. Example function below:
Following authentication, Kinde provides a json web token (jwt) to your application. Along with the standard information we also include the org_code
and the permissions for that organization (this is important as a user can belong to multiple organizations and have different permissions for each).
Example of a returned token:
The id_token
will also contain an array of organizations that a user belongs to - this is useful if you wanted to build out an organization switcher for example.
There are two helper functions you can use to extract information:
For more information about how organizations work in Kinde, see Kinde organizations for developers.
Token storage
Link to this sectionOnce the user has successfully authenticated, you’ll get a JWT and possibly a refresh token that should be stored securely.
SDK API reference
Link to this sectiondomain
Link to this sectionEither your Kinde instance url or your custom domain. e.g. https://yourapp.kinde.com
.
Type: string
Required: Yes
callback_url
Link to this sectionThe url that the user will be returned to after authentication.
Type: string
Required: Yes
client_id
Link to this sectionThe ID of your application in Kinde.
Type: string
Required: Yes
grant_type
Link to this sectionDefine the grant type when using the SDK.
Type: string
Required: Yes
client_secret
Link to this sectionThe unique client secret associated with your application in Kinde.
Type: string
Required: No
code_verifier
Link to this sectionPKCE works by having the app generate a random value at the beginning of the flow called a Code Verifier.
Type: string
Required: No, except for PKCE flow
scope
Link to this sectionThe scopes to be requested from Kinde.
Type: string
Required: No
Default: openid profile email offline
audience
Link to this sectionThe audience claim for the JWT.
Type: string
Required: No
org_code
Link to this sectionAdditional parameters that will be passed in the authorization request.
Type: string
Required: No
KindeSDK methods
Link to this sectionget_login_url
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign in. Optional parameters are used for custom sign-up and sign-in and are documented here.
Arguments (optional):
Usage:
Sample output:
get_register_url
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign up. Optional parameters are used for custom sign-up and sign-in and are documented here.
Arguments (optional):
Usage:
Sample:
logout
Link to this sectionLogs the user out of Kinde.
Arguments:
Usage:
Sample:
fetch_token
Link to this sectionReturns the raw access token from URL after logged in from Kinde.
Arguments:
Usage:
Sample:
refresh_token
Link to this sectionGet new access token from Kinde if existed refresh_token
.
Usage:
create_org
Link to this sectionReturn the redirect URL to sign up and create a new organization in your business.
Usage:
Sample:
get_claim
Link to this sectionGets a claim from an access or ID token.
Arguments:
Usage:
Sample:
get_permission
Link to this sectionReturns the state of a given permission.
Arguments:
Usage:
Sample:
get_permissions
Link to this sectionReturns all permissions for the current user for the organization they are signed into.
Usage:
Sample:
get_organization
Link to this sectionGet details for the organization your user is signed into.
Usage:
Sample:
get_organizations
Link to this sectionGets an array of all organizations the user has access to.
Usage:
Sample:
get_user_details
Link to this sectionReturns the profile for the current user.
Usage:
Sample:
get_flag
Link to this sectionGets a feature flag from an access token.
Arguments:
Usage:
Sample:
get_boolean_flag
Link to this sectionGets a boolean feature flag from an access token.
Arguments:
Usage:
Sample: True
or False
get_string_flag
Link to this sectionGets a string feature flag from an access token.
Arguments:
Usage:
Sample: “pink”
get_integer_flag
Link to this sectionGets a integer feature flag from an access token
Arguments:
Usage:
Sample: 5
is_authenticated()
Link to this sectionTo check user authenticated or not.
Sample: true
or false
If you need help connecting to Kinde, please contact us at support@kinde.com.