Skip to content

Repository files navigation

ChainLearn API

Stellar-based learning platform backend built with Fastify, TypeScript, and Drizzle ORM.

Overview

ChainLearn is a decentralized learning platform where users:

  • Authenticate using their Stellar wallet (SEP-10)
  • Enroll in courses and complete quizzes
  • Earn token rewards for passing quizzes (on-chain via Soroban)
  • Receive NFT credentials for course completion

Tech Stack

LayerTechnology
FrameworkFastify 5
LanguageTypeScript (strict mode)
DatabasePostgreSQL 16 + Drizzle ORM
CacheRedis 7 (ioredis)
BlockchainStellar SDK + Soroban RPC
AuthSEP-10 wallet auth + JWT
ValidationZod
LoggingPino
TestingVitest

Quick Start

Prerequisites

  • Node.js >= 22
  • Docker & Docker Compose
  • A Stellar testnet account (for the platform wallet)

Setup

# Clone and installcd chainlearn-api
npm install
# Start database and cache
docker compose up -d
# Configure environment
cp .env.example .env
# Edit .env with your Stellar keys and database URL# Run migrations
npm run db:generate
npm run db:migrate
# Seed sample data
npm run db:seed
# Start development server
npm run dev

The API will be available at http://localhost:3000.

Environment Variables

VariableDescriptionDefault
NODE_ENVEnvironment modedevelopment
PORTServer port3000
HOSTBind address0.0.0.0
DATABASE_URLPostgreSQL connection stringrequired
REDIS_URLRedis connection stringredis://localhost:6379
JWT_SECRETJWT signing secret (64+ chars / 256-bit, non-placeholder)required
STELLAR_NETWORKtestnet or mainnettestnet
STELLAR_HORIZON_URLHorizon server URLrequired
STELLAR_SOROBAN_RPC_URLSoroban RPC URLrequired
STELLAR_PLATFORM_SECRETPlatform wallet secret keyrequired
STELLAR_QUIZ_CONTRACT_IDQuiz Soroban contract addressrequired
STELLAR_REWARD_CONTRACT_IDReward Soroban contract addressrequired
STELLAR_CREDENTIAL_CONTRACT_IDCredential Soroban contract addressrequired
RATE_LIMIT_MAXMax requests per window100
RATE_LIMIT_WINDOW_MSRate limit window (ms)60000

API Endpoints

Authentication

MethodPathDescription
POST/api/auth/challengeGenerate SEP-10 challenge
POST/api/auth/verifyVerify signed challenge, get JWT

Users

MethodPathDescription
GET/api/users/meGet authenticated user profile
PUT/api/users/meUpdate user profile
GET/api/users/me/progressGet learning progress stats

Courses

MethodPathDescription
GET/api/coursesList available courses
GET/api/courses/:idGet course details
POST/api/courses/:id/enrollEnroll in a course

Quizzes

MethodPathDescription
POST/api/quizzes/generateGenerate a quiz for a module
POST/api/quizzes/:id/submitSubmit quiz answers

Rewards

MethodPathDescription
POST/api/rewards/claimClaim reward for passed quiz
GET/api/rewards/historyGet reward claim history

Credentials

MethodPathDescription
POST/api/credentials/mintMint course completion NFT
GET/api/credentialsList user credentials

Health

MethodPathDescription
GET/healthHealth check

Database Schema

Tables

  • users — Stellar wallet-linked user profiles with learning preferences
  • courses — Course catalog with content references
  • enrollments — User-course enrollment tracking
  • quizzes — Generated quizzes with JSONB question arrays
  • quiz_submissions — Graded answers with reward tracking
  • credentials — NFT certificate records

Migrations

# Generate migration from schema changes
npm run db:generate
# Apply pending migrations
npm run db:migrate
# Open Drizzle Studio (visual DB browser)
npm run db:studio

Project Structure

src/
├── server.ts # Fastify bootstrap + route registration
├── config/ # Environment + service configs
├── modules/ # Feature modules (auth, users, courses, etc.)
│ └── [module]/
│ ├── *.controller.ts # Request handlers
│ ├── *.service.ts # Business logic
│ ├── *.model.ts # Drizzle table references
│ ├── *.routes.ts # Route definitions
│ └── *.types.ts # Zod schemas + TypeScript types
├── stellar/ # Stellar SDK wrappers
├── middleware/ # Auth, validation, rate limiting, error handling
├── database/ # Schema, migrations, seed data
└── utils/ # Logger, crypto, error classes

Scripts

CommandDescription
npm run devStart dev server with hot reload
npm run buildCompile TypeScript
npm startRun compiled production build
npm testRun test suite
npm run lintRun ESLint
npm run typecheckType-check without emitting

Docker

# Build production image
docker build -t chainlearn-api .# Run with environment
docker run -p 3000:3000 --env-file .env chainlearn-api

Testing

# Run all tests
npm test# Watch mode
npm run test:watch

Tests are located in tests/ with unit tests under unit/ and end-to-end API tests under e2e/.

License

MIT

About

Backend API for ChainLearn — course management, user progress, and Stellar integration.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages