Skip to main content

Python

In this document you can find code examples for the Ory Python SDK.

info

Missing an example? Please create a feature request and it will be added here.

You can find more examples of SDK usage in the auto-generated documentation for [ory-client] (https://github.com/ory/sdk/blob/master/clients/client/python/README.md).

Installation

If you are starting from scratch, first set up a new Python project

mkdir myproject
cd myproject

Install the Ory Python SDK

pip install

If the python package is hosted on a repository, you can install directly using:

pip install git+https://github.com/ory/sdk.git

You may need to run pip with root permission: sudo pip install git+https://github.com/ory/sdk.git.

Then import the package:

import ory_client

Setuptools

Install via Setuptools.

python setup.py install --user

Or use sudo python setup.py install to install the package for all users.

Then import the package:

import ory_client

CheckPermission

As an example, let's check for whether a given subject has the necessary permissions.

Example

Bearer Authentication (oryAccessToken):

import time
import ory_client
from ory_client.api import permission_api
from ory_client.model.check_permission_result import CheckPermissionResult
from ory_client.model.error_generic import ErrorGeneric
from pprint import pprint
# Defining the host is optional and defaults to https://playground.projects.oryapis.com
# See configuration.py for a list of all supported configuration parameters.
# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.
# Configure Bearer authorization: oryAccessToken
configuration = ory_client.Configuration(
host = "https://playground.projects.oryapis.com"
access_token = 'YOUR_BEARER_TOKEN'
)

# Enter a context with an instance of the API client
with ory_client.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = permission_api.PermissionApi(api_client)
namespace = "namespace_example" # str | Namespace of the Relationship (optional)
object = "object_example" # str | Object of the Relationship (optional)
relation = "relation_example" # str | Relation of the Relationship (optional)
subject_id = "subject_id_example" # str | SubjectID of the Relationship (optional)
subject_set_namespace = "subject_set.namespace_example" # str | Namespace of the Subject Set (optional)
subject_set_object = "subject_set.object_example" # str | Object of the Subject Set (optional)
subject_set_relation = "subject_set.relation_example" # str | Relation of the Subject Set (optional)
max_depth = 1 # int | (optional)

# example passing only required values which don't have defaults set
# and optional values
try:
# Check a permission
api_response = api_instance.check_permission(namespace=namespace, object=object, relation=relation, subject_id=subject_id, subject_set_namespace=subject_set_namespace, subject_set_object=subject_set_object, subject_set_relation=subject_set_relation, max_depth=max_depth)
pprint(api_response)
except ory_client.ApiException as e:
print("Exception when calling PermissionApi->check_permission: %s\n" % e)