DEV Community

Cover image for Directus Tutorial: A Headless CMS on Your Own Database
The Practical Developer
The Practical Developer

Posted on Edited on Originally published at practicaldevblog.blogspot.com

Directus Tutorial: A Headless CMS on Your Own Database

Quick answer

Directus is an open source headless CMS that sits on top of a database you already own: it reads your tables and gives you an admin panel and a REST API for them, instead of locking your content away. This post installs it on SQLite, models a Karachi clothing shop catalogue, and tests the API with curl. You will see the exact calls, the real replies, and the permission error that stops you without a login token.

A small clothing shop in Karachi keeps its catalogue in a spreadsheet and a WhatsApp chat. New stock arrives, prices change, and the one person who knows the spreadsheet is out for the day. The website, if there is one, still shows last season's prices, because updating it means editing code. The shop does not need a bigger spreadsheet. It needs one place where the stock list lives, a simple screen for the owner to edit it, and an API so the website reads the same list. That is what a headless CMS does, and Directus does it without taking the database away from you.

What I would check before using it

My first check is whether a database with real data already exists. Directus wraps your tables instead of replacing them, so it shines when the shop already keeps stock, orders, or customers in Postgres, MySQL, or SQLite. Starting from nothing, a hosted CMS may get you moving faster. The trade-off is the point: control, and the work that comes with it.

My second check is who edits the content. The admin app is the reason to pick Directus: a shop owner with no code skills can add a kurta, change a price, and fix a typo. If only developers will ever touch the data, a plain API framework may be simpler, and the Express Explained: Node's Minimal Web Framework Guide post covers that route.

My third check is permissions. Out of the box, the public role can do nothing, and every call needs a role that allows it. That is a good default, but plan roles before the frontend team asks why every call returns 403. For how token checks work, the JWT Authentication: Verify a Token Without a Database post is a useful companion.

My fourth check is who runs the server. Directus is free and open source, but self-hosting means you own updates and backups. A shop that cannot afford a dead catalogue page should budget for that, or pay for the hosted cloud version.

My verdict, as judgement: adopt Directus when you already own the data and non-developers need to edit it. It flips when there is no database yet, no one to run a server, or only developers touching the data.

A Karachi clothing shop, run on Directus

The demo is the Karachi clothing shop: three products in a collection called products, added through the API and read back with a filter.

Install Directus into an empty folder. On Node 24, the plain install failed building a native dependency, so I pinned it with an npm override first:

{
  "overrides": {
    "isolated-vm": "6.2.0"
  }
}
Enter fullscreen mode Exit fullscreen mode
$ npm install directus
added 1070 packages in 3m

$ npx directus --version
12.5.1
Enter fullscreen mode Exit fullscreen mode

Directus reads its settings from a .env file. SQLite keeps the demo to one file, and bootstrap creates the admin user. My first bootstrap failed: admin@shop.local is not a valid email address, so I used admin@example.com.

KEY=directus-batch67-demo-key-9f2c
SECRET=directus-batch67-demo-secret-7a1d
DB_CLIENT=sqlite3
DB_FILENAME=./data.db
ADMIN_EMAIL=admin@example.com
ADMIN_PASSWORD=ShopDemo123
PORT=8055
Enter fullscreen mode Exit fullscreen mode
$ npx directus bootstrap
[03:08:55.284] INFO: Setting up first admin role...
[03:08:55.456] INFO: Adding first admin user...
[03:08:55.712] INFO: Done

$ npx directus start
[03:09:08.387] INFO: Server started at http://0.0.0.0:8055
Enter fullscreen mode Exit fullscreen mode

Log in through the API. The reply carries an access_token, a JWT that expires in fifteen minutes. Save it; every call below sends it as a Bearer token:

$ TOKEN=$(curl -s -X POST http://127.0.0.1:8055/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"email":"admin@example.com","password":"ShopDemo123"}' \
  | python3 -c "import sys,json; print(json.load(sys.stdin)['data']['access_token'])")
Enter fullscreen mode Exit fullscreen mode

A collection is a table with metadata. Creating products also creates its id field, so three more calls add the fields the shop needs: name, price in rupees, and stock, all required:

$ curl -s -X POST http://127.0.0.1:8055/collections \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"collection":"products","meta":{"collection":"products","icon":"store","display_template":"{{name}}"},"schema":{"name":"products"}}'
{"data":{"collection":"products","meta":{"collection":"products","icon":"store","display_template":"{{name}}", ... },"schema":{"name":"products","sql":"CREATE TABLE `products` (`id` integer not null primary key autoincrement)"}}}
HTTP 200
Enter fullscreen mode Exit fullscreen mode
$ curl -s -X POST http://127.0.0.1:8055/fields/products \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"field":"name","type":"string","meta":{"interface":"input","required":true},"schema":{"is_nullable":false}}'
HTTP 200
Enter fullscreen mode Exit fullscreen mode

The price_rs and stock fields went in the same way, both HTTP 200. Adding id by hand failed, because the collection already made it:

$ curl -s -X POST http://127.0.0.1:8055/fields/products \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"field":"id","type":"integer","meta":{"interface":"input"},"schema":{"is_primary_key":true,"has_auto_increment":true}}'
{"errors":[{"message":"Invalid payload. Field \"id\" already exists in collection \"products\".","extensions":{"reason":"Field \"id\" already exists in collection \"products\"","code":"INVALID_PAYLOAD"}}]}
HTTP 400
Enter fullscreen mode Exit fullscreen mode

My first item call forgot the token. Directus answered 403, not 401, naming the collection and the missing permission. The fix is the Authorization header, nothing else:

$ curl -s -X POST http://127.0.0.1:8055/items/products \
  -H 'Content-Type: application/json' \
  -d '{"name":"White Cotton Kurta","price_rs":2450,"stock":12}'
{"errors":[{"message":"You don't have permission to access collection \"products\" or it does not exist. Queried in root.","extensions":{"reason":"You don't have permission to access collection \"products\" or it does not exist. Queried in root.","code":"FORBIDDEN"}}]}
HTTP 403
Enter fullscreen mode Exit fullscreen mode

With the token, the three products go in. Directus answers 200, not 201, and hands back each row:

$ curl -s -X POST http://127.0.0.1:8055/items/products \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"name":"White Cotton Kurta","price_rs":2450,"stock":12}'
{"data":{"id":1,"name":"White Cotton Kurta","price_rs":2450,"stock":12}}
HTTP 200

$ curl -s -X POST http://127.0.0.1:8055/items/products \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"name":"Pastel Lawn Kurta","price_rs":1950,"stock":8}'
{"data":{"id":2,"name":"Pastel Lawn Kurta","price_rs":1950,"stock":8}}
HTTP 200

$ curl -s -X POST http://127.0.0.1:8055/items/products \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"name":"Embroidered Lawn Suit","price_rs":3200,"stock":5}'
{"data":{"id":3,"name":"Embroidered Lawn Suit","price_rs":3200,"stock":5}}
HTTP 200
Enter fullscreen mode Exit fullscreen mode

Reading back with a filter is a plain GET. The _gte operator keeps only the two products priced at Rs 2,000 or more; the Rs 1,950 kurta drops out. Note the -g flag: without it, curl treats the brackets as a pattern:

$ curl -s -g "http://127.0.0.1:8055/items/products?filter[price_rs][_gte]=2000&fields=id,name,price_rs,stock" \
  -H "Authorization: Bearer $TOKEN"
{"data":[{"id":1,"name":"White Cotton Kurta","price_rs":2450,"stock":12},{"id":3,"name":"Embroidered Lawn Suit","price_rs":3200,"stock":5}]}
HTTP 200
Enter fullscreen mode Exit fullscreen mode

Terminal output showing a 403 error from posting a product without a login token, then a successful reply confirming product 1, a White Cotton Kurta at Rs 2,450.

Common mistakes and how to fix them

First, calling the API without a token. The symptom is a 403 with "You don't have permission to access collection \"products\" or it does not exist." The fix is to log in and send the token as an Authorization: Bearer header.

Second, adding the id field by hand. The symptom is a 400 with "Invalid payload. Field \"id\" already exists in collection \"products\"." Skip it: creating a collection already adds an auto-incrementing id.

Third, inventing a filter operator. I tried _greater. The symptom was a 400 with "Invalid query. \"integer\" field type does not contain the \"_greater\" filter operator." The fix is to use _gte, _lte, or _eq.

Fourth, leaving out a required field. The symptom is a 400 with "Validation failed for field \"price_rs\". Value is required." The fix is to send every required field, or make the field optional.

Fifth, forgetting -g on a filtered GET. The symptom is curl printing nothing at all, with no error. The fix is curl -g, or URL-encoding the brackets.

Frequently Asked Questions

Does Directus store my data, or do I keep my database? You keep it. Directus sits on top of your tables and reads and writes them, so the data stays where it is. The SQLite Explained: The Database That Lives Inside Your App post covers that database.

Do I need to know Node.js to run Directus? You need Node to install and start it, but the admin app needs no code: the owner edits products from a screen. To test the API without curl, the Postman Explained: Testing APIs Before the Frontend Exists post covers that tool.

Can I use PostgreSQL instead of SQLite? Yes. Change DB_CLIENT to pg, point Directus at your Postgres details, and bootstrap the same way. The calls in this post stay the same.

Is Directus free? The self-hosted version is open source and free to run on your own server, as here. A paid hosted cloud version exists for teams that skip self-hosting.

What to remember

  • Directus wraps your database; it does not own it.
  • A collection is a table with metadata; creating one adds the id field.
  • The public role can do nothing until you grant it, so expect 403s.
  • Send the login token as a Bearer header on every call.
  • Filters use operators like _gte; a wrong one is a 400, and curl needs -g.

Where this leaves you

One SQLite file, one collection, three products, and a Karachi clothing shop now has a catalogue with an admin screen and an API: the Rs 2,450 kurta is product 1, and a filtered read returns exactly the two items over Rs 2,000. The 403 was the useful part, a reminder that Directus denies first and asks for a role. If your stock list still lives in a spreadsheet, point Directus at a copy of that data and let the owner edit it through the admin app before you write a line of frontend code.

Further reading

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Originally published at https://practicaldevblog.blogspot.com/2026/12/directus-shop-cms-tutorial.html

Top comments (0)