A Claude-written graph database in Rust. Use at your own risk.
1# GigaBrain API Documentation 2 3GigaBrain provides both REST and gRPC APIs for graph database operations. This documentation covers both interfaces. 4 5## Table of Contents 6 7- [Authentication](#authentication) 8- [REST API](#rest-api) 9- [gRPC API](#grpc-api) 10- [Error Handling](#error-handling) 11- [Examples](#examples) 12 13## Authentication 14 15GigaBrain uses JWT (JSON Web Tokens) for authentication. Include the token in the `Authorization` header: 16 17``` 18Authorization: Bearer <your-jwt-token> 19``` 20 21### Roles 22 23- **Admin**: Full access to all operations including user management 24- **ReadWrite**: Can read and modify graph data 25- **ReadOnly**: Can only read graph data 26 27## REST API 28 29Base URL: `http://localhost:3000/api/v1` 30 31### Health Check 32 33**GET** `/health` 34 35Returns server health status. 36 37**Response:** 38```json 39{ 40 "status": "healthy", 41 "version": "0.1.0", 42 "uptime_seconds": 3600 43} 44``` 45 46### Nodes 47 48#### Create Node 49 50**POST** `/nodes` 51 52Creates a new node in the graph. 53 54**Request Body:** 55```json 56{ 57 "labels": ["Person", "Employee"], 58 "properties": { 59 "name": "John Doe", 60 "age": 30, 61 "active": true, 62 "salary": 75000.50 63 } 64} 65``` 66 67**Response:** 68```json 69{ 70 "node_id": 12345 71} 72``` 73 74#### Get Node 75 76**GET** `/nodes/{node_id}` 77 78Retrieves a node by its ID. 79 80**Response:** 81```json 82{ 83 "node": { 84 "id": 12345, 85 "labels": ["Person", "Employee"], 86 "properties": { 87 "name": "John Doe", 88 "age": 30, 89 "active": true, 90 "salary": 75000.50 91 } 92 } 93} 94``` 95 96#### Update Node 97 98**PUT** `/nodes/{node_id}` 99 100Updates an existing node's labels and/or properties. Labels are completely replaced if provided, while properties are merged with existing ones. 101 102**Request Body:** 103```json 104{ 105 "labels": ["Person", "Manager"], 106 "properties": { 107 "title": "Engineering Manager", 108 "department": "Technology", 109 "team_size": 8 110 } 111} 112``` 113 114**Response:** 115```json 116{ 117 "id": 12345, 118 "labels": ["Person", "Manager"], 119 "properties": { 120 "name": "John Doe", 121 "age": 30, 122 "title": "Engineering Manager", 123 "department": "Technology", 124 "team_size": 8 125 } 126} 127``` 128 129**Notes:** 130- Labels: If provided, completely replace existing labels 131- Properties: If provided, merge with existing properties (new properties are added, existing ones are updated) 132- Both `labels` and `properties` fields are optional 133 134#### Delete Node Property 135 136**DELETE** `/nodes/{node_id}/properties/{property_key}` 137 138Deletes a specific property from a node. 139 140**Response:** 141```json 142{ 143 "id": 12345, 144 "labels": ["Person", "Manager"], 145 "properties": { 146 "name": "John Doe", 147 "age": 30, 148 "department": "Technology" 149 } 150} 151``` 152 153**Error Response (404):** 154```json 155{ 156 "error": "Property 'salary' not found", 157 "code": 404 158} 159``` 160 161#### Delete Node 162 163**DELETE** `/nodes/{node_id}` 164 165Deletes a node from the graph. 166 167**Response:** 168- Status: `204 No Content` 169- Body: Empty 170 171### Relationships 172 173#### Create Relationship 174 175**POST** `/relationships` 176 177Creates a new relationship between two nodes. 178 179**Request Body:** 180```json 181{ 182 "start_node": { 183 "id": 12345 184 }, 185 "end_node": { 186 "id": 67890 187 }, 188 "rel_type": "WORKS_FOR", 189 "properties": [ 190 { 191 "key": "since", 192 "value": { 193 "string_value": "2023-01-01" 194 } 195 } 196 ] 197} 198``` 199 200**Response:** 201```json 202{ 203 "relationship_id": { 204 "id": 98765 205 } 206} 207``` 208 209#### Get Relationship 210 211**GET** `/relationships/{relationship_id}` 212 213Retrieves a relationship by its ID. 214 215**Response:** 216```json 217{ 218 "relationship": { 219 "id": { 220 "id": 98765 221 }, 222 "start_node": { 223 "id": 12345 224 }, 225 "end_node": { 226 "id": 67890 227 }, 228 "rel_type": "WORKS_FOR", 229 "properties": [] 230 } 231} 232``` 233 234#### Delete Relationship 235 236**DELETE** `/relationships/{relationship_id}` 237 238Deletes a relationship from the graph. 239 240**Response:** 241```json 242{ 243 "success": true 244} 245``` 246 247### Cypher Queries 248 249#### Execute Cypher Query 250 251**POST** `/cypher` 252 253Executes a Cypher query against the graph. 254 255**Request Body:** 256```json 257{ 258 "query": "MATCH (n:Person) WHERE n.age > 25 RETURN n.name, n.age", 259 "parameters": {} 260} 261``` 262 263**Response:** 264```json 265{ 266 "results": [ 267 { 268 "columns": ["n.name", "n.age"], 269 "data": [ 270 ["John Doe", 30], 271 ["Jane Smith", 28] 272 ] 273 } 274 ], 275 "error": "", 276 "execution_time_ms": 15 277} 278``` 279 280### Graph Algorithms 281 282#### Shortest Path 283 284**POST** `/algorithms/shortest-path` 285 286Finds the shortest path between two nodes. 287 288**Request Body:** 289```json 290{ 291 "start_node": { 292 "id": 12345 293 }, 294 "end_node": { 295 "id": 67890 296 }, 297 "max_depth": 10 298} 299``` 300 301**Response:** 302```json 303{ 304 "path": [ 305 {"id": 12345}, 306 {"id": 54321}, 307 {"id": 67890} 308 ], 309 "total_weight": 2.0 310} 311``` 312 313#### PageRank 314 315**POST** `/algorithms/pagerank` 316 317Calculates PageRank scores for specified nodes. 318 319**Request Body:** 320```json 321{ 322 "nodes": [ 323 {"id": 12345}, 324 {"id": 67890} 325 ], 326 "damping_factor": 0.85, 327 "max_iterations": 100, 328 "tolerance": 0.0001 329} 330``` 331 332**Response:** 333```json 334{ 335 "rankings": { 336 "12345": 0.15, 337 "67890": 0.25 338 } 339} 340``` 341 342#### Centrality 343 344**POST** `/algorithms/centrality` 345 346Calculates various centrality measures. 347 348**Request Body:** 349```json 350{ 351 "algorithm": "betweenness", 352 "nodes": [ 353 {"id": 12345}, 354 {"id": 67890} 355 ] 356} 357``` 358 359**Response:** 360```json 361{ 362 "centrality": { 363 "12345": 0.35, 364 "67890": 0.42 365 } 366} 367``` 368 369#### Community Detection 370 371**POST** `/algorithms/community-detection` 372 373Detects communities in the graph. 374 375**Request Body:** 376```json 377{ 378 "algorithm": "louvain", 379 "resolution": 1.0 380} 381``` 382 383**Response:** 384```json 385{ 386 "communities": [ 387 [12345, 23456, 34567], 388 [67890, 78901, 89012] 389 ], 390 "modularity": 0.45 391} 392``` 393 394### Graph Statistics 395 396#### Get Graph Stats 397 398**GET** `/stats` 399 400Returns statistics about the graph. 401 402**Response:** 403```json 404{ 405 "node_count": 1000, 406 "relationship_count": 2500, 407 "relationship_types": ["KNOWS", "WORKS_FOR", "LIVES_IN"], 408 "labels": ["Person", "Company", "City"] 409} 410``` 411 412### API Documentation 413 414#### Get API Documentation 415 416**GET** `/docs` 417 418Returns this API documentation in JSON format. 419 420### Schema Constraints 421 422#### Create Constraint 423 424**POST** `/constraints` 425 426Creates a new schema constraint to enforce data integrity rules. 427 428**Request Body:** 429```json 430{ 431 "constraint_type": "required", 432 "label": "Employee", 433 "property": "email" 434} 435``` 436 437**Constraint Types:** 438 4391. **Required Constraint** 440```json 441{ 442 "constraint_type": "required", 443 "label": "Employee", 444 "property": "email" 445} 446``` 447 4482. **Type Constraint** 449```json 450{ 451 "constraint_type": "type", 452 "label": "Employee", 453 "property": "salary", 454 "type_value": "integer" 455} 456``` 457Supported types: `string`, `integer`, `float`, `boolean` 458 4593. **Range Constraint** 460```json 461{ 462 "constraint_type": "range", 463 "label": "Employee", 464 "property": "salary", 465 "min_value": 30000, 466 "max_value": 200000 467} 468``` 469 4704. **Pattern Constraint** 471```json 472{ 473 "constraint_type": "pattern", 474 "label": "User", 475 "property": "email", 476 "pattern": "@" 477} 478``` 479 4805. **Enum Constraint** 481```json 482{ 483 "constraint_type": "enum", 484 "label": "Task", 485 "property": "status", 486 "allowed_values": ["pending", "in_progress", "completed"] 487} 488``` 489 490**Response:** 491```json 492{ 493 "id": "required:Employee:email", 494 "constraint_type": "required", 495 "label": "Employee", 496 "property": "email", 497 "details": {} 498} 499``` 500 501#### List Constraints 502 503**GET** `/constraints` 504 505Lists all defined schema constraints. 506 507**Response:** 508```json 509[ 510 { 511 "id": "required:Employee:email", 512 "constraint_type": "required", 513 "label": "Employee", 514 "property": "email", 515 "details": {} 516 }, 517 { 518 "id": "type:Employee:salary", 519 "constraint_type": "type", 520 "label": "Employee", 521 "property": "salary", 522 "details": { 523 "type": "Integer" 524 } 525 } 526] 527``` 528 529## gRPC API 530 531The gRPC API provides the same functionality as the REST API but uses Protocol Buffers for serialization. The service is defined in `proto/gigabrain.proto`. 532 533### Service Definition 534 535```protobuf 536service GigaBrainService { 537 // Node operations 538 rpc CreateNode(CreateNodeRequest) returns (CreateNodeResponse); 539 rpc GetNode(GetNodeRequest) returns (GetNodeResponse); 540 rpc UpdateNode(UpdateNodeRequest) returns (UpdateNodeResponse); 541 rpc DeleteNode(DeleteNodeRequest) returns (DeleteNodeResponse); 542 543 // Relationship operations 544 rpc CreateRelationship(CreateRelationshipRequest) returns (CreateRelationshipResponse); 545 rpc GetRelationship(GetRelationshipRequest) returns (GetRelationshipResponse); 546 rpc DeleteRelationship(DeleteRelationshipRequest) returns (DeleteRelationshipResponse); 547 548 // Query operations 549 rpc ExecuteCypher(CypherQueryRequest) returns (CypherQueryResponse); 550 551 // Algorithm operations 552 rpc ShortestPath(ShortestPathRequest) returns (ShortestPathResponse); 553 rpc PageRank(PageRankRequest) returns (PageRankResponse); 554 rpc Centrality(CentralityRequest) returns (CentralityResponse); 555 rpc CommunityDetection(CommunityDetectionRequest) returns (CommunityDetectionResponse); 556 557 // Stats operations 558 rpc GetGraphStats(GraphStatsRequest) returns (GraphStatsResponse); 559} 560``` 561 562### Connection 563 564Connect to the gRPC server at `localhost:50051`. 565 566### Property Values 567 568Property values can be one of the following types: 569 570- `string_value`: String data 571- `int_value`: 64-bit integer 572- `float_value`: 64-bit floating point 573- `bool_value`: Boolean 574- `bytes_value`: Binary data 575 576## Error Handling 577 578### HTTP Status Codes 579 580- `200 OK`: Successful operation 581- `400 Bad Request`: Invalid request data 582- `401 Unauthorized`: Authentication required 583- `403 Forbidden`: Insufficient permissions 584- `404 Not Found`: Resource not found 585- `500 Internal Server Error`: Server error 586 587### Error Response Format 588 589```json 590{ 591 "error": "Node ID is required", 592 "code": 400 593} 594``` 595 596### gRPC Status Codes 597 598- `OK`: Successful operation 599- `INVALID_ARGUMENT`: Invalid request parameters 600- `NOT_FOUND`: Resource not found 601- `INTERNAL`: Internal server error 602- `UNAUTHENTICATED`: Authentication failed 603- `PERMISSION_DENIED`: Authorization failed 604 605## Examples 606 607### Creating a Social Network 608 609```bash 610# Create users 611curl -X POST http://localhost:3000/api/v1/nodes \ 612 -H "Content-Type: application/json" \ 613 -H "Authorization: Bearer <token>" \ 614 -d '{ 615 "labels": ["Person"], 616 "properties": { 617 "name": "Alice", 618 "age": 25 619 } 620 }' 621 622curl -X POST http://localhost:3000/api/v1/nodes \ 623 -H "Content-Type: application/json" \ 624 -H "Authorization: Bearer <token>" \ 625 -d '{ 626 "labels": ["Person"], 627 "properties": { 628 "name": "Bob", 629 "age": 30 630 } 631 }' 632 633# Create friendship 634curl -X POST http://localhost:3000/api/v1/relationships \ 635 -H "Content-Type: application/json" \ 636 -H "Authorization: Bearer <token>" \ 637 -d '{ 638 "start_node": 1, 639 "end_node": 2, 640 "rel_type": "FRIENDS", 641 "properties": { 642 "since": "2023-01-01" 643 } 644 }' 645 646# Find mutual friends 647curl -X POST http://localhost:3000/api/v1/cypher \ 648 -H "Content-Type: application/json" \ 649 -H "Authorization: Bearer <token>" \ 650 -d '{ 651 "query": "MATCH (a:Person)-[:FRIENDS]-(mutual)-[:FRIENDS]-(b:Person) WHERE a.name = $name1 AND b.name = $name2 RETURN mutual.name", 652 "parameters": {"name1": "Alice", "name2": "Bob"} 653 }' 654 655# Update a node 656curl -X PUT http://localhost:3000/api/v1/nodes/1 \ 657 -H "Content-Type: application/json" \ 658 -H "Authorization: Bearer <token>" \ 659 -d '{ 660 "labels": ["Person", "VIP"], 661 "properties": { 662 "status": "premium", 663 "last_seen": "2023-12-01" 664 } 665 }' 666 667# Delete a specific property 668curl -X DELETE http://localhost:3000/api/v1/nodes/1/properties/age \ 669 -H "Authorization: Bearer <token>" 670``` 671 672### Working with Schema Constraints 673 674```bash 675# Create a required email constraint for Employee nodes 676curl -X POST http://localhost:3000/api/v1/constraints \ 677 -H "Content-Type: application/json" \ 678 -H "Authorization: Bearer <token>" \ 679 -d '{ 680 "constraint_type": "required", 681 "label": "Employee", 682 "property": "email" 683 }' 684 685# Create a type constraint for salary 686curl -X POST http://localhost:3000/api/v1/constraints \ 687 -H "Content-Type: application/json" \ 688 -H "Authorization: Bearer <token>" \ 689 -d '{ 690 "constraint_type": "type", 691 "label": "Employee", 692 "property": "salary", 693 "type_value": "integer" 694 }' 695 696# Create a range constraint for salary 697curl -X POST http://localhost:3000/api/v1/constraints \ 698 -H "Content-Type: application/json" \ 699 -H "Authorization: Bearer <token>" \ 700 -d '{ 701 "constraint_type": "range", 702 "label": "Employee", 703 "property": "salary", 704 "min_value": 30000, 705 "max_value": 200000 706 }' 707 708# Try to create an employee without email (will fail) 709curl -X POST http://localhost:3000/api/v1/nodes \ 710 -H "Content-Type: application/json" \ 711 -H "Authorization: Bearer <token>" \ 712 -d '{ 713 "labels": ["Employee"], 714 "properties": { 715 "name": "John Doe", 716 "salary": 50000 717 } 718 }' 719# Response: {"error": "Validation error: Required property 'email' is missing", "code": 400} 720 721# Create an employee with all required properties 722curl -X POST http://localhost:3000/api/v1/nodes \ 723 -H "Content-Type: application/json" \ 724 -H "Authorization: Bearer <token>" \ 725 -d '{ 726 "labels": ["Employee"], 727 "properties": { 728 "name": "John Doe", 729 "email": "john@example.com", 730 "salary": 50000 731 } 732 }' 733``` 734 735### Running Graph Algorithms 736 737```bash 738# Calculate shortest path 739curl -X POST http://localhost:3000/api/v1/algorithms/shortest-path \ 740 -H "Content-Type: application/json" \ 741 -H "Authorization: Bearer <token>" \ 742 -d '{ 743 "start_node": 1, 744 "end_node": 10, 745 "relationship_types": ["FRIENDS", "KNOWS"] 746 }' 747 748# Run PageRank 749curl -X POST http://localhost:3000/api/v1/algorithms/pagerank \ 750 -H "Content-Type: application/json" \ 751 -H "Authorization: Bearer <token>" \ 752 -d '{ 753 "nodes": [1, 2, 3], 754 "damping_factor": 0.85 755 }' 756``` 757 758## Performance Considerations 759 760- Use batch operations when creating multiple nodes/relationships 761- Enable request compression for large payloads 762- Consider using gRPC for high-performance scenarios 763- Implement connection pooling for production use 764- Use streaming for large result sets 765 766## Rate Limiting 767 768The API implements rate limiting to prevent abuse: 769 770- **Default limit**: 1000 requests per minute per IP 771- **Burst limit**: 100 requests per second 772- **Headers**: `X-RateLimit-Remaining`, `X-RateLimit-Reset` 773 774## Monitoring 775 776Monitor API performance using the built-in metrics: 777 778- Request count and duration 779- Error rates by endpoint 780- Active connections 781- Memory and CPU usage 782 783Access metrics at: `GET /metrics` (Prometheus format)