Directus
Directus Quickstart
Setting Up Users with Full Access
Create an Admin Role (if not already exists)
- Go to Settings > User Roles
- Click “Create Role”
- Name it “Admin” or “Full Access”
- Under “Policies”, Add a new Policy
- Under the “New Policy’s -> Permissions” add all your table collections and grant full access.
a. Alternatively click “Admin Access -> Enabled”
b. Click “App Access -> Enabled” if you want to grant the policy access to directus tables too.
- Save the policy and the role.
Create a User with Full Access
- Go to User Directory
- Click “Create User”
- Fill in user details (email, password)
- Go to “Admin Options” and assign the “Full Access” role you created
- Make sure to create your user and “Access Token” for REST authentication.
- Save the user
Making Models Accessible via API
For each collection (table), you need to configure permissions:
- Go to Settings > Data Model
- Select the Data Model you want to configure
- For each collection (Customer, Video, Stock, etc.):
- Toggle “Make Collection Visible”
- Save Model
API Examples
REST API Examples
Get All Videos
| curl -X GET 'http://127.0.0.1:8055/items/Video' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
|
Get a Specific Video by ID
| curl -X GET 'http://127.0.0.1:8055/items/Video/1' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
|
Create a New Customer
| curl -X POST 'http://127.0.0.1:8055/items/Customer' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"customer_id": 4,
"first_name": "Mark",
"last_name": "Zuckerberg",
"contact_info": {"email": "mark@meta.com", "phone": "555-0044"},
"address_info": {"address": "1 Hacker Way, Menlo Park, CA"},
"membership_status": 1
}'
|
Update a Stock Item
| curl -X PATCH 'http://127.0.0.1:8055/items/Stock/1' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"format_type": "Bluray"
}'
|
Filter Videos by MPAA Rating
| curl -X GET --globoff 'http://127.0.0.1:8055/items/Video?filter[mpaa_rating][_eq]=R' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
|
GraphQL Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | query {
Video {
video_id
title
mpaa_rating
stock {
stock_id
format_type
supplier_id {
name
price
}
}
}
}
|
Create a Transaction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | mutation {
create_Transaction(
item: {
transaction_id: 1
stock_id: 1
customer_id: 1
transaction_date: "2023-05-01T12:00:00Z"
transaction_type: "rental"
price: 4.99
due_date: "2023-05-08T12:00:00Z"
condition: "good"
}
) {
transaction_id
}
}
|
Get Customers with Their Transactions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | query {
Customer {
customer_id
first_name
last_name
transactions {
transaction_id
transaction_date
stock_id {
video_id {
title
}
}
}
}
}
|
Directus-Specific REST API Details
Directus automatically creates REST endpoints based on your table names. For your Blockbuster database:
- Collections (tables) become pluralized endpoints:
/items/Customer
/items/Video
/items/Stock
/items/Transaction
- etc.
Common Query Parameters:
fields
- Select specific fields: ?fields=title,release_date
filter
- Filter results: ?filter[mpaa_rating][_eq]=R
sort
- Sort results: ?sort=-release_date
limit
- Limit results: ?limit=10
offset
- Pagination: ?offset=20
search
- Full-text search: ?search=shawshank
Authentication:
Directus uses JWT tokens. Include in headers:
| Authorization: Bearer YOUR_ACCESS_TOKEN
|
Handling Relationships:
Directus automatically handles relationships. For example, to get Videos with their Stock:
| curl -X GET 'http://127.0.0.1:8055/items/Stock?fields=*,video_id.*' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
|