Skip to content

Directus

Directus Quickstart

Setting Up Users with Full Access

Create an Admin Role (if not already exists)

  1. Go to Settings > User Roles
  2. Click “Create Role”
  3. Name it “Admin” or “Full Access”
  4. Under “Policies”, Add a new Policy
  5. 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.
  6. Save the policy and the role.

Create a User with Full Access

  1. Go to User Directory
  2. Click “Create User”
  3. Fill in user details (email, password)
  4. Go to “Admin Options” and assign the “Full Access” role you created
  5. Make sure to create your user and “Access Token” for REST authentication.
  6. Save the user

Making Models Accessible via API

For each collection (table), you need to configure permissions:

  1. Go to Settings > Data Model
  2. Select the Data Model you want to configure
  3. For each collection (Customer, Video, Stock, etc.):
  4. Toggle “Make Collection Visible”
  5. Save Model

API Examples

REST API Examples

Get All Videos
1
2
curl -X GET 'http://127.0.0.1:8055/items/Video' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Get a Specific Video by ID
1
2
curl -X GET 'http://127.0.0.1:8055/items/Video/1' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Create a New Customer
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
1
2
3
4
5
6
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
1
2
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:

1
Authorization: Bearer YOUR_ACCESS_TOKEN

Handling Relationships:

Directus automatically handles relationships. For example, to get Videos with their Stock:

1
2
curl -X GET 'http://127.0.0.1:8055/items/Stock?fields=*,video_id.*' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'