API Reference
Part of Matsushiba Systems
Complete API documentation for MatsushibaDB with examples and code snippets.
Authenticate user and receive JWT token
POST /auth/login
Content-Type: application/json
{
"username": "admin",
"password": "password"
}
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"username": "admin",
"role": "admin",
"permissions": ["read", "write", "admin"]
},
"expires_in": 3600
}
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{
"success": false,
"error": "Invalid credentials",
"code": "AUTH_INVALID_CREDENTIALS"
}
Invalidate JWT token
POST /auth/logout
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"message": "Logged out successfully"
}
Get current user information
GET /auth/me
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"user": {
"id": 1,
"username": "admin",
"role": "admin",
"permissions": ["read", "write", "admin"],
"created_at": "2025-01-01T00:00:00Z",
"last_login": "2025-01-24T10:30:00Z"
}
}
Execute SQL query
POST /db/query
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
{
"query": "SELECT * FROM users WHERE id = ?",
"params": [123]
}
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"data": [
{
"id": 123,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2025-01-01T00:00:00Z"
}
],
"meta": {
"rows_affected": 1,
"execution_time": 5,
"query_id": "q_123456789"
}
}
Execute multiple queries in a transaction
POST /db/transaction
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
{
"queries": [
{
"query": "INSERT INTO users (name, email) VALUES (?, ?)",
"params": ["John Doe", "john@example.com"]
},
{
"query": "INSERT INTO profiles (user_id, bio) VALUES (?, ?)",
"params": ["$last_insert_id", "Software Developer"]
}
]
}
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"results": [
{
"success": true,
"data": null,
"meta": {
"rows_affected": 1,
"last_insert_id": 124
}
},
{
"success": true,
"data": null,
"meta": {
"rows_affected": 1,
"last_insert_id": 125
}
}
],
"transaction_id": "tx_123456789"
}
Get database schema information
GET /db/schema
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"schema": {
"tables": [
{
"name": "users",
"columns": [
{
"name": "id",
"type": "INTEGER",
"primary_key": true,
"not_null": true
},
{
"name": "name",
"type": "TEXT",
"primary_key": false,
"not_null": true
},
{
"name": "email",
"type": "TEXT",
"primary_key": false,
"not_null": false,
"unique": true
}
],
"indexes": [
{
"name": "idx_users_email",
"columns": ["email"],
"unique": true
}
]
}
]
}
}
Get system statistics
GET /admin/stats
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"stats": {
"server": {
"uptime": 86400,
"version": "1.0.8",
"start_time": "2025-01-23T10:30:00Z"
},
"database": {
"size": "50MB",
"tables": 5,
"indexes": 8,
"connections": 5
},
"performance": {
"queries_per_second": 150,
"average_response_time": 5,
"total_queries": 12960000
},
"memory": {
"used": "25MB",
"free": "75MB",
"cache_hits": 0.95
}
}
}
List all users (admin only)
GET /admin/users
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"users": [
{
"id": 1,
"username": "admin",
"role": "admin",
"created_at": "2025-01-01T00:00:00Z",
"last_login": "2025-01-24T10:30:00Z",
"active": true
},
{
"id": 2,
"username": "user1",
"role": "user",
"created_at": "2025-01-02T00:00:00Z",
"last_login": "2025-01-24T09:15:00Z",
"active": true
}
]
}
Create new user (admin only)
POST /admin/users
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
{
"username": "newuser",
"password": "securepassword",
"role": "user"
}
HTTP/1.1 201 Created
Content-Type: application/json
{
"success": true,
"user": {
"id": 3,
"username": "newuser",
"role": "user",
"created_at": "2025-01-24T10:30:00Z",
"active": true
}
}
Get audit logs (admin only)
GET /admin/audit?limit=100&offset=0&user=admin&action=SELECT
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
HTTP/1.1 200 OK
Content-Type: application/json
{
"success": true,
"audit_logs": [
{
"id": 1,
"timestamp": "2025-01-24T10:30:00Z",
"user": "admin",
"action": "SELECT",
"table": "users",
"query": "SELECT * FROM users WHERE id = ?",
"parameters": [123],
"ip": "192.168.1.100",
"success": true,
"execution_time": 5
}
],
"pagination": {
"total": 1000,
"limit": 100,
"offset": 0,
"has_more": true
}
}
Real-time communication via WebSocket
const ws = new WebSocket('ws://localhost:8001', {
headers: {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
}
});
// Request
{
"id": "req_123",
"type": "query",
"data": {
"query": "SELECT * FROM users",
"params": []
}
}
// Response
{
"id": "req_123",
"type": "query_result",
"success": true,
"data": [
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
],
"meta": {
"execution_time": 5,
"rows_affected": 1
}
}
query - Execute SQL querysubscribe - Subscribe to table changesunsubscribe - Unsubscribe from table changesping - Keep-alive pingpong - Keep-alive pong// Subscribe to table changes
ws.send(JSON.stringify({
"id": "sub_123",
"type": "subscribe",
"data": {
"table": "users",
"events": ["insert", "update", "delete"]
}
}));
// Receive real-time updates
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'table_change') {
console.log('Table changed:', message.data);
}
};
Binary protocol for high-performance communication
const net = require('net');
const client = new net.Socket();
client.connect(8002, 'localhost', () => {
console.log('Connected to MatsushibaDB TCP server');
});
// Binary message format
// Header (8 bytes)
// [4 bytes: message length][4 bytes: message type]
// Message types
const MESSAGE_TYPES = {
QUERY: 1,
QUERY_RESULT: 2,
ERROR: 3,
AUTH: 4,
AUTH_RESULT: 5,
PING: 6,
PONG: 7
};
// Query message
const queryMessage = Buffer.concat([
Buffer.from([0, 0, 0, 20]), // Length: 20 bytes
Buffer.from([0, 0, 0, 1]), // Type: QUERY
Buffer.from('SELECT * FROM users', 'utf8')
]);
// Send authentication
const authMessage = Buffer.concat([
Buffer.from([0, 0, 0, 20]), // Length: 20 bytes
Buffer.from([0, 0, 0, 4]), // Type: AUTH
Buffer.from('admin:password', 'utf8')
]);
client.write(authMessage);
AUTH_INVALID_CREDENTIALS
Invalid username or password
AUTH_TOKEN_EXPIRED
JWT token has expired
AUTH_TOKEN_INVALID
Invalid or malformed JWT token
AUTH_INSUFFICIENT_PERMISSIONS
User lacks required permissions
DB_QUERY_SYNTAX_ERROR
Invalid SQL syntax
DB_CONSTRAINT_VIOLATION
Database constraint violation
DB_TABLE_NOT_FOUND
Table does not exist
DB_COLUMN_NOT_FOUND
Column does not exist
SYS_CONNECTION_LIMIT
Maximum connections exceeded
SYS_RATE_LIMIT_EXCEEDED
Rate limit exceeded
SYS_DATABASE_LOCKED
Database is locked
SYS_INTERNAL_ERROR
Internal server error
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"success": false,
"error": "Invalid SQL syntax",
"code": "DB_QUERY_SYNTAX_ERROR",
"details": {
"query": "SELCT * FROM users",
"position": 5,
"suggestion": "Did you mean 'SELECT'?"
},
"timestamp": "2025-01-24T10:30:00Z",
"request_id": "req_123456789"
}