# Directus SDK for Python
[](https://badge.fury.io/py/directus-py-sdk)
[](https://opensource.org/licenses/MIT)
[](https://pypi.org/project/directus-py-sdk/)
A Python SDK for interacting with Directus, an open-source headless CMS and API platform.
## About Directus
[Directus](https://directus.io/) 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:
```bash
pip install directus-py-sdk
```
## Usage
Here are some examples of how to use the Directus Python SDK:
### Initialize the Client
```python
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
```python
# 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
```python
# 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
```python
# 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](https://docs.directus.io/reference/filter-rules.html)
### Collection and Item Management
```python
# 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:
```python
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:
```python
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](https://github.com/Jason-CKY/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
Raw data
{
"_id": null,
"home_page": "https://github.com/Dembrane/directus-py-sdk",
"name": "directus-py-sdk",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Dembrane",
"author_email": "info@dembrane.com",
"download_url": "https://files.pythonhosted.org/packages/5c/98/1905fa04e0d9639cf412bdaad55c08d1b8e308962c13c80d98cb6f1d0eb3/directus_py_sdk-1.2.0.tar.gz",
"platform": null,
"description": "# Directus SDK for Python\n\n[](https://badge.fury.io/py/directus-py-sdk)\n[](https://opensource.org/licenses/MIT)\n[](https://pypi.org/project/directus-py-sdk/)\n\nA Python SDK for interacting with Directus, an open-source headless CMS and API platform.\n\n## About Directus\n\n[Directus](https://directus.io/) is a powerful and flexible open-source headless CMS and API platform. It provides a\nuser-friendly interface for managing content and a robust API for integrating with other applications. Directus allows\nyou to create and customize your data models, manage users and permissions, and easily expose your data through a\nRESTful API.\n\n## About the library\n\nThis library provides a Python SDK for interacting with Directus. You can use it to perform various operations such as\nmanaging users, files, collections, and items. The SDK simplifies the process of interacting with the Directus API by\nproviding a set of methods that you can use to perform common tasks.\n\n## New features (1.1.0)\n\n- Enhanced authentication handling with token expiration support\n- New `me()` method to get current user information\n- Improved file handling with automatic file type detection\n- New `DirectusQueryBuilder` for constructing complex queries\n- SQL to Directus query converter (`SQLToDirectusConverter`)\n- Better error handling and response processing\n- Enhanced image transformation capabilities\n- Improved URL handling and cleaning\n\n## Installation\n\nYou can install the Directus Python SDK using pip:\n\n```bash\npip install directus-py-sdk\n```\n\n## Usage\n\nHere are some examples of how to use the Directus Python SDK:\n\n### Initialize the Client\n\n```python\nfrom directus_py_sdk import DirectusClient\n\nclient = DirectusClient(url='https://your-directus-instance.com', token='your_access_token')\n```\n\n### Authentication with email and password if needed\n\n```python\n# Login with email and password\nclient.login(email='user@example.com', password='password')\n\n# Get current user information\nme = client.me()\n\n# Refresh token\nclient.refresh()\n\n# Logout\nclient.logout()\n```\n\n### Users Management\n\n```python\n# Get all users\nusers = client.get_users()\n\n# Create a new user\nuser_data = {\n 'first_name': 'John',\n 'last_name': 'Doe',\n 'email': 'john@example.com',\n 'password': 'password'\n}\nnew_user = client.create_user(user_data)\n\n# Update a user\nupdated_user = client.update_user(user_id='user_id', user_data={'first_name': 'Updated Name'})\n\n# Delete a user\nclient.delete_user(user_id='user_id')\n```\n\n### Files Management\n\n```python\n# Get all files\nfiles = client.get_files()\n\n# Search files with a filter\nrequest = {\n \"query\": {\n \"filter\": {\n \"title\": {\n \"_icontains\": \"my search request\" # Search for files with \"my search request\" in the title\n }\n }\n }\n}\nitems = client.get_files(request)\n\n\n# Suppose you get an item and it's a photo, you can get the URL of the photo with the following code\nphoto_url = client.get_file_url(items[0]['id'])\n\n# It's possible to transform or add some display options to the photo URL\ndisplay = {\n \"quality\": 95, # Quality of the image\n}\ntransform = [\n [\"blur\", 10], # Blur the image\n [\"tint\", \"rgb(255, 0, 0)\"] # Tint the image in red\n]\n\nphoto_url = client.get_file_url(items[0]['id'], display=display, transform=transform)\n\n# Download the file on the disk\nclient.download_photo(items[0]['id'], 'path/to/download.jpg', display=display, transform=transform)\n\n\n# Download a file other than a photo\nclient.download_file(items[0]['id'], 'path/to/download.jpg')\n\n\n# Upload a file\ndata = {\n \"title\": \"Readme\",\n \"description\": \"Readme file\",\n \"tags\": ['readme', 'file'],\n}\nfile = client.upload_file(\"readme.md\", data)\n\n# Delete a file\nclient.delete_file(file_id='file_id')\n```\n\nInformation about filter requests can be found in\nthe [Directus API documentation](https://docs.directus.io/reference/filter-rules.html)\n\n### Collection and Item Management\n\n```python\n# Get a collection\ncollection = client.get_collection(collection_name='your_collection')\n\n# List all items and filter the results\ncollection = \"my_collection\"\nrequest = {\n \"query\": {\n # More information about filter requests can be found in the Directus API documentation (https://docs.directus.io/reference/filter-rules.html)\n \"filter\": {\n \"col_name\": {\n \"_icontains\": \"inverness\" # Search inverness in the col_name column\n }\n }\n }\n}\nitems = client.get_items(collection, request)\n\n\n# Get an item from a collection\nitem = client.get_item(collection_name='your_collection', item_id='item_id')\n\n# Create a new item in a collection\nitem_data = {\n 'title': 'New Item',\n 'description': 'This is a new item'\n}\nnew_item = client.create_item(collection_name='your_collection', item_data=item_data)\n\n# Update an item in a collection\nupdated_item = client.update_item(collection_name='your_collection', item_id='item_id',\n item_data={'title': 'Updated Title'})\n\n# Delete an item from a collection\nclient.delete_item(collection_name='your_collection', item_id='item_id')\n```\n\n## Using DirectusQueryBuilder\n\nThe new DirectusQueryBuilder provides a fluent interface for constructing complex queries:\n\n```python\nfrom directus_py_sdk import DirectusQueryBuilder, DOp\n\n# Create a builder instance\nbuilder = DirectusQueryBuilder()\n\n# Build a complex query\nquery = (builder\n .field(\"status\", DOp.EQUALS, \"published\")\n .or_condition([\n {\"author\": {DOp.EQUALS: \"john\"}},\n {\"category\": {DOp.IN: [\"news\", \"tech\"]}}\n ])\n .sort(\"date_created\", \"-title\")\n .limit(10)\n .offset(0)\n .build())\n\n# Use the query\nitems = client.get_items(\"articles\", query)\n```\n\n## SQL to Directus Query Converter\n\nFor 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:\n\n```python\nfrom directus_py_sdk import SQLToDirectusConverter\n\nconverter = SQLToDirectusConverter()\n\nsql_query = \"\"\"\nSELECT * FROM articles\nWHERE status = 'published'\nAND (author = 'john' OR category IN ('news', 'tech'))\nORDER BY date_created ASC, title DESC\n\"\"\"\n\ndirectus_query = converter.convert(sql_query)\nitems = client.get_items(\"articles\", directus_query)\n\n```\n\n## Contributing\n\nContributions are welcome! If you find any issues or have suggestions for improvements, please:\n\n- Fork the repository\n- Create a new branch for your feature\n- Submit a pull request\n\n## License\n\nThis project is licensed under the MIT License.\n\n## Acknowledgements\n\nThis library was inspired by the [directus-py-sdkthon](https://github.com/Jason-CKY/directus-py-sdkthon) project, which\nis also released under the MIT License. Special thanks to the contributors of that project for their work.\n\n\n## Future scope\n1. Add builder pattern\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python SDK for interacting with Directus API (colletion, items, users, files)",
"version": "1.2.0",
"project_urls": {
"Homepage": "https://github.com/Dembrane/directus-py-sdk"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "cf330685667dd096b51d06bd568d01fb4493ee39b7b02d7d9a41bf49bc6e1d04",
"md5": "696ddbe634f46a1690e1474a7596f46d",
"sha256": "cc40930837b179feade4ca1214e886e783173e9c34a5ae8b8e3b76ec4611d8ca"
},
"downloads": -1,
"filename": "directus_py_sdk-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "696ddbe634f46a1690e1474a7596f46d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 15330,
"upload_time": "2025-08-01T07:36:21",
"upload_time_iso_8601": "2025-08-01T07:36:21.051629Z",
"url": "https://files.pythonhosted.org/packages/cf/33/0685667dd096b51d06bd568d01fb4493ee39b7b02d7d9a41bf49bc6e1d04/directus_py_sdk-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5c981905fa04e0d9639cf412bdaad55c08d1b8e308962c13c80d98cb6f1d0eb3",
"md5": "2633e42b8ee3e03a925b0e2673c8c46f",
"sha256": "9d06eac3ea16209f09e2a46e270ede51b1190591adc4dce64f97d7525ca31aa3"
},
"downloads": -1,
"filename": "directus_py_sdk-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "2633e42b8ee3e03a925b0e2673c8c46f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 17303,
"upload_time": "2025-08-01T07:36:23",
"upload_time_iso_8601": "2025-08-01T07:36:23.823856Z",
"url": "https://files.pythonhosted.org/packages/5c/98/1905fa04e0d9639cf412bdaad55c08d1b8e308962c13c80d98cb6f1d0eb3/directus_py_sdk-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-01 07:36:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Dembrane",
"github_project": "directus-py-sdk",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "requests",
"specs": []
},
{
"name": "urllib3",
"specs": []
},
{
"name": "sqlparse",
"specs": []
},
{
"name": "ruff",
"specs": []
},
{
"name": "mypy",
"specs": []
},
{
"name": "types-requests",
"specs": []
},
{
"name": "types-urllib3",
"specs": []
}
],
"lcname": "directus-py-sdk"
}