Quick Start
This guide walks you through creating a complete content API with Trokky in under 10 minutes.
What We’ll Build
Section titled “What We’ll Build”A simple blog API with:
- Post and Author content types
- REST API endpoints
- Admin interface (Studio)
- File-based storage
Step 1: Project Setup
Section titled “Step 1: Project Setup”Create a new directory and initialize:
mkdir my-trokky-projectcd my-trokky-projectnpm init -ynpm pkg set type=moduleInstall dependencies:
npm install @trokky/core @trokky/express @trokky/adapter-filesystem @trokky/studio expressnpm install -D typescript tsx @types/express @types/nodeStep 2: Define Your Schemas
Section titled “Step 2: Define Your Schemas”Create schemas.ts:
import { Schema } from '@trokky/core';
export const schemas: Schema[] = [ { name: 'author', title: 'Author', fields: [ { name: 'name', title: 'Name', type: 'string', required: true, }, { name: 'email', title: 'Email', type: 'string', validation: { email: true }, }, { name: 'bio', title: 'Biography', type: 'text', }, ], }, { name: 'post', title: 'Blog Post', fields: [ { name: 'title', title: 'Title', type: 'string', required: true, }, { name: 'slug', title: 'Slug', type: 'slug', options: { source: 'title' }, }, { name: 'author', title: 'Author', type: 'reference', options: { to: 'author' }, }, { name: 'content', title: 'Content', type: 'richtext', }, { name: 'publishedAt', title: 'Published Date', type: 'datetime', }, { name: 'featured', title: 'Featured Post', type: 'boolean', default: false, }, ], },];Step 3: Create the Server
Section titled “Step 3: Create the Server”Create server.ts:
import express from 'express';import { TrokkyExpress } from '@trokky/express';import { schemas } from './schemas.js';
const app = express();
// Initialize Trokkyconst trokky = await TrokkyExpress.create({ schemas, storage: { adapter: 'filesystem', contentDir: './content', }, security: { adminUser: { username: 'admin', password: 'demo123', }, }, studio: { enabled: true, branding: { title: 'My Blog CMS', }, }, server: { cors: { origin: 'http://localhost:5173', credentials: true, }, },});
// Mount all Trokky routestrokky.mount(app);
// Start serverconst PORT = process.env.PORT || 3000;app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); console.log(`Studio available at http://localhost:${PORT}/studio`); console.log(`API available at http://localhost:${PORT}/api`);});Step 4: Run the Server
Section titled “Step 4: Run the Server”npx tsx server.tsYou should see:
Server running at http://localhost:3000Studio available at http://localhost:3000/studioAPI available at http://localhost:3000/apiStep 5: Explore the Studio
Section titled “Step 5: Explore the Studio”Open http://localhost:3000/studio in your browser.
- Log in with username
adminand passworddemo123 - You’ll see your content types: Authors and Posts
- Create an author
- Create a blog post and link it to the author
Step 6: Use the API
Section titled “Step 6: Use the API”The REST API is now available at /api. Try these endpoints:
List all posts
Section titled “List all posts”curl http://localhost:3000/api/documents/postGet a specific post
Section titled “Get a specific post”curl http://localhost:3000/api/documents/post/{id}Create a post (authenticated)
Section titled “Create a post (authenticated)”curl -X POST http://localhost:3000/api/documents/post \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "title": "My First Post", "content": "Hello, Trokky!", "featured": true }'Project Structure
Section titled “Project Structure”After running, your project looks like this:
my-trokky-project/├── content/│ ├── author/│ │ └── {id}.json│ └── post/│ └── {id}.json├── schemas.ts├── server.ts├── package.json└── node_modules/Content is stored as JSON files, making it easy to inspect, edit, and version control.
What’s Next?
Section titled “What’s Next?”You’ve built a complete content API! Here are some next steps:
- Core Concepts - Understand how Trokky works
- Defining Schemas - Create complex content structures
- Studio Customization - Customize the admin interface
- Deployment - Deploy to production
Full Example Code
Section titled “Full Example Code”Find the complete example in the Trokky repository.