A RESTful API for Non-Violent Communication (NVC) exercises, built with Bun and Elysia.js. This API provides multilingual access to various NVC exercises including observation vs evaluation, feelings vs thoughts, needs vs demands, listening barriers, requests, gratitude, and conflict resolution exercises.
- API Key Authentication: Secure access with API key-based authentication
- Multilingual Support: English and Chinese translations
- Exercise Categories: 7 different types of NVC exercises
- Filtering: Filter by type, difficulty, and target audience
- SQLite Database: Lightweight, file-based storage
- CORS Enabled: Ready for web applications
- Type Safety: Built with TypeScript
- Bun installed on your system
-
Clone or download the project
-
Install dependencies:
bun install
-
Initialize the database:
bun run init-db
-
Create an API key:
bun run api-keys create "My App" -
Start the development server:
bun run dev
The API will be available at http://localhost:3000
This API uses API key-based authentication. You must include a valid API key in the Authorization header for all requests except health checks.
Authorization: Bearer nvc_your_api_key_here
# Create a new API key
bun run api-keys create "My Application"
# List all API keys
bun run api-keys list
# Deactivate an API key
bun run scripts/manage_api_keys.ts deactivate 1http://localhost:3000
Retrieve a list of exercises with optional filtering.
Query Parameters:
type(optional): Filter by exercise typelang(optional): Language for responses (enorzh)difficulty(optional): Filter by difficulty (beginner,intermediate,advanced)targetAudience(optional): Filter by audience (individual,group)
Example Requests:
# Get all exercises
curl -H "Authorization: Bearer nvc_your_api_key_here" \
http://localhost:3000/exercises
# Get listening barrier exercises in Chinese
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises?type=listening-barriers&lang=zh"
# Get beginner exercises for individuals
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises?difficulty=beginner&targetAudience=individual"Retrieve a single exercise by ID.
Path Parameters:
id: Exercise ID (numeric string)
Query Parameters:
lang(optional): Language for response (enorzh)
Example Requests:
# Get exercise with ID 1
curl -H "Authorization: Bearer nvc_your_api_key_here" \
http://localhost:3000/exercises/1
# Get exercise with ID 1 in Chinese
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises/1?lang=zh"Health check endpoint.
Example Request:
curl http://localhost:3000/health- observation-evaluation: Distinguishing observations from evaluations
- feelings-thoughts: Identifying genuine feelings vs thoughts
- needs-demands: Expressing needs without making demands
- listening-barriers: Common barriers to empathetic listening
- requests: Making clear, specific requests
- gratitude: Expressing appreciation in NVC format
- conflict-resolution: Step-by-step conflict resolution
{
"id": "1",
"type": "listening-barriers",
"name": "Advising",
"description": "Giving advice or solutions without being asked",
"difficulty": "beginner",
"targetAudience": "individual",
"relatedExercises": ["2", "3"],
"example": "You should just talk to your boss about it.",
"nvcAlternative": "It sounds like you're feeling frustrated..."
}{
"id": "1",
"type": "listening-barriers",
"name": {
"en": "Advising",
"zh": "建议"
},
"description": {
"en": "Giving advice or solutions without being asked",
"zh": "未经请求就给出建议或解决方案"
},
"difficulty": "beginner",
"targetAudience": "individual",
"relatedExercises": ["2", "3"],
"example": {
"en": "You should just talk to your boss about it.",
"zh": "你应该直接和你的老板谈谈。"
},
"nvcAlternative": {
"en": "It sounds like you're feeling frustrated...",
"zh": "听起来你在工作中感到很沮丧..."
}
}{
"error": "Missing Authorization header"
}{
"error": "Invalid API key"
}{
"error": "Invalid language. Use 'en' or 'zh'."
}{
"error": "Exercise not found"
}{
"error": "Internal server error"
}const API_KEY = 'nvc_your_api_key_here';
const headers = { 'Authorization': `Bearer ${API_KEY}` };
// Fetch all listening barrier exercises
const response = await fetch('http://localhost:3000/exercises?type=listening-barriers', {
headers
});
const exercises = await response.json();
// Fetch a specific exercise in Chinese
const exercise = await fetch('http://localhost:3000/exercises/1?lang=zh', {
headers
});
const data = await exercise.json();import requests
API_KEY = 'nvc_your_api_key_here'
headers = {'Authorization': f'Bearer {API_KEY}'}
# Get all exercises
response = requests.get('http://localhost:3000/exercises', headers=headers)
exercises = response.json()
# Get gratitude exercises in Chinese
response = requests.get('http://localhost:3000/exercises',
params={'type': 'gratitude', 'lang': 'zh'},
headers=headers
)
gratitude_exercises = response.json()# Get all observation-evaluation exercises
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises?type=observation-evaluation"
# Get intermediate difficulty exercises
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises?difficulty=intermediate"
# Get group exercises in Chinese
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises?targetAudience=group&lang=zh"
# Get a specific exercise
curl -H "Authorization: Bearer nvc_your_api_key_here" \
"http://localhost:3000/exercises/5"The API uses SQLite for data storage. The database file (exercises.db) is automatically created and seeded with sample exercises on first run.
The API comes with 10 sample exercises covering all exercise types:
- 3 Listening barrier exercises
- 2 Observation vs evaluation exercises
- 1 Feelings vs thoughts exercise
- 1 Needs vs demands exercise
- 1 Requests exercise
- 1 Gratitude exercise
- 1 Conflict resolution exercise
nvc-api/
├── src/
│ ├── index.ts # Main API server
│ └── database.ts # Database setup and operations
├── package.json
├── tsconfig.json
└── README.md
# Development server with auto-reload
bun run dev
# Initialize database
bun run init-db
# API key management
bun run api-keys create "App Name"
bun run api-keys list
# Type checking
bun run tsc --noEmit- Runtime: Bun
- Framework: Elysia.js
- Database: SQLite (bun:sqlite)
- Language: TypeScript
- CORS: @elysiajs/cors
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
# Create a new API key
bun run api-keys create "Mobile App"
# List all API keys (shows metadata, not actual keys)
bun run api-keys list
# Deactivate an API key
bun run scripts/manage_api_keys.ts deactivate 1For programmatic API key management:
POST /admin/api-keys- Create new API keyGET /admin/api-keys- List all API keysDELETE /admin/api-keys/:id- Deactivate API key
- Store API keys in environment variables
- Never commit API keys to version control
- Rotate keys regularly
- Use separate keys for different applications
- Monitor usage via the
last_usedtimestamp
For questions or issues, please create an issue in the project repository.
For detailed authentication documentation, see API_AUTHENTICATION.md.