Skip to main content

Directus SDK for Python

PyPI version License: MIT Python Version

A Python SDK for interacting with Directus, an open-source headless CMS and API platform.

About Directus

Directus is a powerful and flexible open-source headless CMS and API platform. It provides a user-friendly interface for managing content and a robust API for integrating with other applications. Directus allows you to create and customize your data models, manage users and permissions, and easily expose your data through a RESTful API.

About the library

This library provides a Python SDK for interacting with Directus. You can use it to perform various operations such as managing users, files, collections, and items. The SDK simplifies the process of interacting with the Directus API by providing a set of methods that you can use to perform common tasks.

New features (1.1.0)

  • Enhanced authentication handling with token expiration support
  • New me() method to get current user information
  • Improved file handling with automatic file type detection
  • New DirectusQueryBuilder for constructing complex queries
  • SQL to Directus query converter (SQLToDirectusConverter)
  • Better error handling and response processing
  • Enhanced image transformation capabilities
  • Improved URL handling and cleaning

Installation

You can install the Directus Python SDK using pip:

pip install directus-py-sdk

Usage

Here are some examples of how to use the Directus Python SDK:

Initialize the Client

from directus_py_sdk import DirectusClient

client = DirectusClient(url='https://your-directus-instance.com', token='your_access_token')

Authentication with email and password if needed

# Login with email and password
client.login(email='user@example.com', password='password')

# Get current user information
me = client.me()

# Refresh token
client.refresh()

# Logout
client.logout()

Users Management

# Get all users
users = client.get_users()

# Create a new user
user_data = {
    'first_name': 'John',
    'last_name': 'Doe',
    'email': 'john@example.com',
    'password': 'password'
}
new_user = client.create_user(user_data)

# Update a user
updated_user = client.update_user(user_id='user_id', user_data={'first_name': 'Updated Name'})

# Delete a user
client.delete_user(user_id='user_id')

Files Management

# Get all files
files = client.get_files()

# Search files with a filter
request = {
    "query": {
        "filter": {
            "title": {
                "_icontains": "my search request" # Search for files with "my search request" in the title
            }
        }
    }
}
items = client.get_files(request)


# Suppose you get an item and it's a photo, you can get the URL of the photo with the following code
photo_url = client.get_file_url(items[0]['id'])

# It's possible to transform or add some display options to the photo URL
display = {
    "quality": 95, # Quality of the image
}
transform = [
    ["blur", 10], # Blur the image
    ["tint", "rgb(255, 0, 0)"] # Tint the image in red
]

photo_url = client.get_file_url(items[0]['id'], display=display, transform=transform)

# Download the file on the disk
client.download_photo(items[0]['id'], 'path/to/download.jpg', display=display, transform=transform)


# Download a file other than a photo
client.download_file(items[0]['id'], 'path/to/download.jpg')


# Upload a file
data = {
    "title": "Readme",
    "description": "Readme file",
    "tags": ['readme', 'file'],
}
file = client.upload_file("readme.md", data)

# Delete a file
client.delete_file(file_id='file_id')

Information about filter requests can be found in the Directus API documentation

Collection and Item Management

# Get a collection
collection = client.get_collection(collection_name='your_collection')

# List all items and filter the results
collection = "my_collection"
request = {
    "query": {
        # More information about filter requests can be found in the Directus API documentation (https://docs.directus.io/reference/filter-rules.html)
        "filter": {
            "col_name": {
                "_icontains": "inverness" # Search inverness in the col_name column
            }
        }
    }
}
items = client.get_items(collection, request)


# Get an item from a collection
item = client.get_item(collection_name='your_collection', item_id='item_id')

# Create a new item in a collection
item_data = {
    'title': 'New Item',
    'description': 'This is a new item'
}
new_item = client.create_item(collection_name='your_collection', item_data=item_data)

# Update an item in a collection
updated_item = client.update_item(collection_name='your_collection', item_id='item_id',
                                  item_data={'title': 'Updated Title'})

# Delete an item from a collection
client.delete_item(collection_name='your_collection', item_id='item_id')

Using DirectusQueryBuilder

The new DirectusQueryBuilder provides a fluent interface for constructing complex queries:

from directus_py_sdk import DirectusQueryBuilder, DOp

# Create a builder instance
builder = DirectusQueryBuilder()

# Build a complex query
query = (builder
    .field("status", DOp.EQUALS, "published")
    .or_condition([
        {"author": {DOp.EQUALS: "john"}},
        {"category": {DOp.IN: ["news", "tech"]}}
    ])
    .sort("date_created", "-title")
    .limit(10)
    .offset(0)
    .build())

# Use the query
items = client.get_items("articles", query)

SQL to Directus Query Converter

For those like me, like to use SQL instead of Directus query language, you can use the SQLToDirectusConverter to convert your SQL queries to Directus query format:

from directus_py_sdk import SQLToDirectusConverter

converter = SQLToDirectusConverter()

sql_query = """
SELECT * FROM articles
WHERE status = 'published'
AND (author = 'john' OR category IN ('news', 'tech'))
ORDER BY date_created ASC, title DESC
"""

directus_query = converter.convert(sql_query)
items = client.get_items("articles", directus_query)

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please:

  • Fork the repository
  • Create a new branch for your feature
  • Submit a pull request

License

This project is licensed under the MIT License.

Acknowledgements

This library was inspired by the directus-py-sdkthon project, which is also released under the MIT License. Special thanks to the contributors of that project for their work.

Future scope

  1. Add builder pattern

Release files for directus-py-sdk 1.2.0

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

Source distribution (sdist)

Source distribution for directus-py-sdk 1.2.0
File Size Uploaded
directus_py_sdk-1.2.0.tar.gz 17.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for directus-py-sdk 1.2.0
File Interpreter ABI Platform
directus_py_sdk-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.6 kB

Release files / directus_py_sdk-1.2.0.tar.gz

Download URL directus_py_sdk-1.2.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9d06eac3ea16209f09e2a46e270ede51b1190591adc4dce64f97d7525ca31aa3
BLAKE2b-256 checksum
How to use checksums
5c981905fa04e0d9639cf412bdaad55c08d1b8e308962c13c80d98cb6f1d0eb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / directus_py_sdk-1.2.0-py3-none-any.whl

Download URL directus_py_sdk-1.2.0-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cc40930837b179feade4ca1214e886e783173e9c34a5ae8b8e3b76ec4611d8ca
BLAKE2b-256 checksum
How to use checksums
cf330685667dd096b51d06bd568d01fb4493ee39b7b02d7d9a41bf49bc6e1d04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.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