decapod 0.48.2

Decapod is a Rust-built governance runtime for AI agents: repo-native state, enforced workflow, proof gates, safe coordination.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# API_DESIGN.md - API Design Standards

**Authority:** guidance (comprehensive API design with exact specifications, schemas, and patterns)
**Layer:** Architecture
**Binding:** No
**Scope:** REST, GraphQL, gRPC API design with exact specifications for pre-inference context

---

## 1. REST API Design

### 1.1 Resource Naming Conventions

```yaml
# Rules:
# - Use nouns, not verbs (GET /users not GET /getUsers)
# - Use plural for collections (/users not /user)
# - Use kebab-case for multi-word paths (/user-profiles not /userProfiles)
# - Nest resources for relationships (max 2 levels deep)
# - Use query parameters for filtering, sorting, pagination

# Good examples:
GET    /users                    # List users
GET    /users/{userId}           # Get single user
POST   /users                    # Create user
PUT    /users/{userId}           # Full update (replace)
PATCH  /users/{userId}           # Partial update
DELETE /users/{userId}           # Delete user
GET    /users/{userId}/orders    # User's orders (nested)
GET    /users/{userId}/orders/{orderId}  # Specific order

# Bad examples:
GET /getUser?id=123              # Verb in path
GET /user/123                    # Singular
POST /createUser                 # Verb in path
DELETE /user/123/orders/all      # 3 levels deep

# Query parameters:
GET /users?status=active&sort=created_at:desc&limit=20&offset=0
GET /orders?created_after=2024-01-01&total_gt=100
GET /products?category=electronics&in_stock=true
GET /users?search=john&fields=id,name,email
```

### 1.2 HTTP Methods

```yaml
GET    # Retrieve resource(s) - idempotent, no body
POST   # Create new resource - not idempotent
PUT    # Replace resource entirely - idempotent
PATCH  # Partial update - idempotent (with proper semantics)
DELETE # Remove resource - idempotent
HEAD   # Like GET but headers only
OPTIONS # CORS preflight, supported methods

# Safe methods: GET, HEAD, OPTIONS (don't modify server state)
# Idempotent methods: GET, PUT, DELETE, HEAD, OPTIONS
# (Idempotent = same request = same result, even if called multiple times)
```

### 1.3 Complete Request/Response Examples

#### Create Resource (POST)
```http
POST /v1/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
Accept: application/json
X-Request-ID: f47ac10b-58cc-4372-a567-0e02b2c3d479
X-Correlation-ID: abc123

{
  "data": {
    "type": "users",
    "attributes": {
      "email": "john.doe@example.com",
      "name": "John Doe",
      "role": "engineer",
      "department": "engineering",
      "metadata": {
        "hire_date": "2024-01-15",
        "location": "New York"
      }
    },
    "relationships": {
      "manager": {
        "data": { "type": "users", "id": "usr_789xyz" }
      },
      "teams": {
        "data": [
          { "type": "teams", "id": "team_alpha" },
          { "type": "teams", "id": "team_beta" }
        ]
      }
    }
  }
}
```

```http
HTTP/1.1 201 Created
Content-Type: application/vnd.api+json
Location: /v1/users/usr_abc123
X-Request-ID: f47ac10b-58cc-4372-a567-0e02b2c3d479
ETag: "v1"
Cache-Control: no-cache

{
  "data": {
    "id": "usr_abc123",
    "type": "users",
    "links": {
      "self": "/v1/users/usr_abc123"
    },
    "attributes": {
      "email": "john.doe@example.com",
      "name": "John Doe",
      "role": "engineer",
      "department": "engineering",
      "created_at": "2024-01-15T10:30:00Z",
      "updated_at": "2024-01-15T10:30:00Z",
      "metadata": {
        "hire_date": "2024-01-15",
        "location": "New York"
      }
    },
    "relationships": {
      "manager": {
        "links": {
          "related": "/v1/users/usr_abc123/manager"
        },
        "data": { "type": "users", "id": "usr_789xyz" }
      },
      "teams": {
        "links": {
          "related": "/v1/users/usr_abc123/teams"
        },
        "data": [
          { "type": "teams", "id": "team_alpha" },
          { "type": "teams", "id": "team_beta" }
        ]
      }
    },
    "meta": {
      "created_by": "usr_system",
      "version": 1
    }
  },
  "included": [
    {
      "id": "usr_789xyz",
      "type": "users",
      "attributes": {
        "name": "Jane Manager"
      }
    },
    {
      "id": "team_alpha",
      "type": "teams",
      "attributes": {
        "name": "Platform Team"
      }
    },
    {
      "id": "team_beta",
      "type": "teams",
      "attributes": {
        "name": "Infrastructure Team"
      }
    }
  ]
}
```

#### Get Resource with Filtering (GET)
```http
GET /v1/users/usr_abc123?include=manager,teams&fields[users]=id,name,email,role HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
```

```http
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
ETag: "v3"
Last-Modified: Mon, 15 Jan 2024 11:45:00 GMT
Cache-Control: private, max-age=300

{
  "data": {
    "id": "usr_abc123",
    "type": "users",
    "attributes": {
      "name": "John Doe",
      "email": "john.doe@example.com",
      "role": "engineer"
    },
    "relationships": {
      "manager": {
        "data": { "type": "users", "id": "usr_789xyz" }
      },
      "teams": {
        "data": [
          { "type": "teams", "id": "team_alpha" },
          { "type": "teams", "id": "team_beta" }
        ]
      }
    }
  },
  "included": [
    {
      "id": "usr_789xyz",
      "type": "users",
      "attributes": {
        "name": "Jane Manager",
        "email": "jane@example.com"
      }
    },
    {
      "id": "team_alpha",
      "type": "teams",
      "attributes": {
        "name": "Platform Team"
      }
    }
  ]
}
```

### 1.4 Pagination Specifications

#### Cursor-based Pagination (Preferred for large datasets)
```http
GET /v1/orders?page[limit]=25&page[cursor]=eyJpZCI6MTIzfQ== HTTP/1.1
```

```json
{
  "data": [...],
  "pagination": {
    "cursors": {
      "before": "eyJpZCI6MTAwfQ==",
      "after": "eyJpZCI6MTI1fQ=="
    },
    "has_more": true,
    "total": null
  },
  "links": {
    "first": "/v1/orders?page[limit]=25",
    "next": "/v1/orders?page[limit]=25&page[cursor]=eyJpZCI6MTI1fQ==",
    "prev": "/v1/orders?page[limit]=25&page[cursor]=eyJpZCI6MTAwfQ=="
  }
}
```

#### Offset-based Pagination (Simple use cases)
```http
GET /v1/users?page[limit]=20&page[offset]=0&page[number]=1 HTTP/1.1
```

```json
{
  "data": [...],
  "pagination": {
    "limit": 20,
    "offset": 0,
    "total": 1500,
    "current_page": 1,
    "total_pages": 75
  },
  "links": {
    "first": "/v1/users?page[limit]=20&page[offset]=0",
    "next": "/v1/users?page[limit]=20&page[offset]=20",
    "prev": null,
    "last": "/v1/users?page[limit]=20&page[offset]=1480"
  }
}
```

#### Keyset Pagination (For extreme performance)
```http
# Use compound sort keys for stable pagination
GET /v1/events?sort=created_at,id&after_id=evt_123&limit=50
# After getting results, use last item's sort keys for next page:
GET /v1/events?sort=created_at,id&after_created_at=2024-01-15T10:30:00Z&after_id=evt_456&limit=50
```

### 1.5 Response Envelope Patterns

```json
{
  "data": {...} | [...],  // Single resource or array
  "meta": {
    "request_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
    "timestamp": "2024-01-15T10:30:00Z",
    "api_version": "v1",
    "pagination": {...} | null,
    "count": 150,
    "filters_applied": {
      "status": "active",
      "created_after": "2024-01-01"
    }
  },
  "error": null | {...},
  "included": [...],
  "links": {...}
}
```

### 1.6 Error Response Patterns

```json
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Request validation failed",
    "details": [
      {
        "field": "email",
        "code": "INVALID_FORMAT",
        "message": "Email format is invalid",
        "value": "not-an-email"
      },
      {
        "field": "age",
        "code": "OUT_OF_RANGE",
        "message": "Age must be between 0 and 150",
        "value": -5
      }
    ],
    "source": {
      "pointer": "/data/attributes/email",
      "parameter": "email"
    },
    "documentation_url": "https://api.example.com/docs/errors/VALIDATION_ERROR",
    "trace_id": "abc123",
    "request_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479"
  },
  "meta": {
    "timestamp": "2024-01-15T10:30:00Z"
  }
}
```

#### HTTP Status Codes
```yaml
# 2xx Success
200 OK                    # GET, PUT, PATCH succeeded
201 Created               # POST created new resource
202 Accepted             # Async operation queued
204 No Content           # DELETE succeeded, no body

# 4xx Client Errors
400 Bad Request           # Malformed request, invalid syntax
401 Unauthorized          # No/invalid authentication
403 Forbidden             # Authenticated but not authorized
404 Not Found             # Resource doesn't exist
405 Method Not Allowed    # HTTP method not supported
409 Conflict              # State conflict (duplicate, version mismatch)
410 Gone                   # Resource permanently deleted
422 Unprocessable Entity  # Validation failed (semantic errors)
429 Too Many Requests     # Rate limit exceeded

# 5xx Server Errors
500 Internal Server Error # Unexpected error
501 Not Implemented       # Feature not implemented
502 Bad Gateway            # Upstream/service failure
503 Service Unavailable    # Temporarily unavailable
504 Gateway Timeout        # Upstream timeout
```

---

## 2. GraphQL API Design

### 2.1 Schema Design

```graphql
# schema.graphql

scalar DateTime
scalar JSON
scalar UUID

enum UserRole {
  ADMIN
  ENGINEER
  MANAGER
  VIEWER
}

enum OrderStatus {
  PENDING
  PROCESSING
  SHIPPED
  DELIVERED
  CANCELLED
}

type User {
  id: ID!
  email: String!
  name: String!
  role: UserRole!
  
  # Relations
  manager: User
  directReports: [User!]!
  orders: OrderConnection!
  
  # Computed
  fullName: String!
  isActive: Boolean!
  
  # Timestamps
  createdAt: DateTime!
  updatedAt: DateTime!
  
  # Meta
  metadata: JSON
}

type Order {
  id: ID!
  status: OrderStatus!
  total: Decimal!
  currency: String!
  
  # Relations
  user: User!
  items: [OrderItem!]!
  
  # Timestamps
  createdAt: DateTime!
  updatedAt: DateTime!
}

type OrderItem {
  id: ID!
  quantity: Int!
  unitPrice: Decimal!
  totalPrice: Decimal!
  product: Product!
}

type Product {
  id: ID!
  name: String!
  description: String
  price: Decimal!
  inStock: Boolean!
  category: Category!
}

type Category {
  id: ID!
  name: String!
  slug: String!
  parent: Category
  children: [Category!]!
  products: ProductConnection!
}

# Pagination
type UserConnection {
  edges: [UserEdge!]!
  pageInfo: PageInfo!
  totalCount: Int!
}

type UserEdge {
  node: User!
  cursor: String!
}

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  startCursor: String
  endCursor: String
}

# Input types
input CreateUserInput {
  email: String!
  name: String!
  role: UserRole = VIEWER
  metadata: JSON
}

input UpdateUserInput {
  email: String
  name: String
  role: UserRole
  metadata: JSON
}

input UserFilterInput {
  role: UserRole
  search: String
  createdAfter: DateTime
  createdBefore: DateTime
}

input OrderByInput {
  field: OrderSortField!
  direction: SortDirection = ASC
}

enum OrderSortField {
  CREATED_AT
  UPDATED_AT
  TOTAL
}

enum SortDirection {
  ASC
  DESC
}
```

### 2.2 Complete Query/Mutation Examples

```graphql
# Query with nested relations and pagination
query GetUserWithOrders($userId: ID!, $orderLimit: Int = 10) {
  user(id: $userId) {
    id
    email
    name
    role
    manager {
      id
      name
      email
    }
    orders(first: $orderLimit, after: null, sort: [{ field: CREATED_AT, direction: DESC }]) {
      edges {
        node {
          id
          status
          total
          currency
          createdAt
          items {
            id
            quantity
            product {
              id
              name
            }
          }
        }
        cursor
      }
      pageInfo {
        hasNextPage
        endCursor
      }
    }
  }
}

# Variables:
{
  "userId": "usr_abc123",
  "orderLimit": 20
}

# Mutation with input and error handling
mutation CreateOrder($input: CreateOrderInput!) {
  createOrder(input: $input) {
    order {
      id
      status
      total
      items {
        id
        quantity
        product {
          id
          name
        }
      }
    }
    user {
      id
      email
      loyaltyPoints
    }
    errors {
      field
      message
      code
    }
  }
}

# Input:
{
  "input": {
    "userId": "usr_abc123",
    "items": [
      { "productId": "prod_xyz", "quantity": 2 },
      { "productId": "prod_abc", "quantity": 1 }
    ],
    "shippingAddress": {
      "street": "123 Main St",
      "city": "New York",
      "state": "NY",
      "zip": "10001",
      "country": "US"
    }
  }
}

# Response:
{
  "data": {
    "createOrder": {
      "order": {
        "id": "ord_123",
        "status": "PENDING",
        "total": "149.99",
        "items": [
          {
            "id": "item_1",
            "quantity": 2,
            "product": { "id": "prod_xyz", "name": "Widget Pro" }
          }
        ]
      },
      "user": {
        "id": "usr_abc123",
        "email": "user@example.com",
        "loyaltyPoints": 150
      },
      "errors": null
    }
  }
}
```

### 2.3 DataLoader Pattern (N+1 Prevention)

```python
# DataLoader: Batch and cache database queries to prevent N+1
from dataloader import DataLoader
from functools import cached_property

class UserLoader(DataLoader):
    @cached_property
    def batch_load_fn(self):
        async def batch_load(ids):
            users = await User.query.where(User.id.in_(ids)).fetch_all()
            return [next((u for u in users if u.id == id), None) for id in ids]
        return batch_load

class OrderLoader(DataLoader):
    @cached_property
    def batch_load_fn(self):
        async def batch_load(user_ids):
            orders = await Order.query.where(Order.user_id.in_(user_ids)).fetch_all()
            # Group by user_id
            orders_by_user = {}
            for order in orders:
                if order.user_id not in orders_by_user:
                    orders_by_user[order.user_id] = []
                orders_by_user[order.user_id].append(order)
            return [orders_by_user.get(uid, []) for uid in user_ids]
        return batch_load

# Usage in resolver
class UserType:
    @staticmethod
    async def resolve_orders(user, info):
        loader = info.context.loaders.order_loader
        return await loader.load(user.id)
```

### 2.4 GraphQL Error Handling

```python
# Custom error types
class GraphQLError(Exception):
    def __init__(self, message, code, field=None, details=None):
        self.message = message
        self.code = code
        self.field = field
        self.details = details or {}

# Union type for errors
class Error:
    pass

class ValidationError(Error):
    field: str
    message: str

class NotFoundError(Error):
    message: str

class UnauthorizedError(Error):
    message: str

type CreateOrderResult {
    order: Order
    errors: [ValidationError!]
}

# Use in mutation
async def resolve_create_order(_, info, input):
    errors = []
    
    # Validate input
    if not input.get('userId'):
        errors.append({'field': 'userId', 'message': 'Required'})
    
    # Check product availability
    for item in input.get('items', []):
        product = await get_product(item.productId)
        if not product:
            errors.append({
                'field': f'items.{item.productId}',
                'message': 'Product not found'
            })
    
    if errors:
        return {'order': None, 'errors': errors}
    
    # Create order
    order = await order_service.create(input)
    return {'order': order, 'errors': None}
```

---

## 3. gRPC & Protocol Buffers

### 3.1 Proto Schema Design

```protobuf
// user_service.proto
syntax = "proto3";

package user.v1;

import "google/protobuf/timestamp.proto";
import "google/protobuf/field_mask.proto";
import "google/protobuf/empty.proto";
import "validate/validate.proto";

option go_package = "github.com/example/user/v1;userpb";

// Service definition
service UserService {
  // Unary RPC
  rpc GetUser(GetUserRequest) returns (User);
  
  // Server streaming
  rpc ListUsers(ListUsersRequest) returns (stream User);
  
  // Client streaming
  rpc CreateUsers(stream CreateUserRequest) returns (CreateUsersResponse);
  
  // Bidirectional streaming
  rpc StreamUserUpdates(StreamUserUpdatesRequest) returns (stream User);
  
  // Batch operations
  rpc BatchGetUsers(BatchGetUsersRequest) returns (BatchGetUsersResponse);
}

message User {
  string id = 1 [(validate.rules).string = {
    min_len: 3,
    max_len: 50,
    pattern: "^usr_[a-zA-Z0-9]+$"
  }];
  
  string email = 2 [
    (validate.rules).string.email = true,
    (validate.rules).string.ignore_empty = false
  ];
  
  string name = 3 [(validate.rules).string = {
    min_len: 1,
    max_len: 200
  }];
  
  UserRole role = 4 [(validate.rules).enum.defined_only = true];
  
  map<string, string> metadata = 5;
  
  google.protobuf.Timestamp created_at = 6;
  google.protobuf.Timestamp updated_at = 7;
}

enum UserRole {
  USER_ROLE_UNSPECIFIED = 0;
  USER_ROLE_VIEWER = 1;
  USER_ROLE_ENGINEER = 2;
  USER_ROLE_MANAGER = 3;
  USER_ROLE_ADMIN = 4;
}

// Request/Response messages
message GetUserRequest {
  string id = 1;
  oneof identifier {
    string user_id = 2;
    string email = 3;
  }
  // Field selection
  google.protobuf.FieldMask field_mask = 4;
}

message ListUsersRequest {
  int32 page_size = 1 [(validate.rules).int32 = {
    gte: 1,
    lte: 100
  }];
  string page_token = 2;
  string filter = 3 [(validate.rules).string.max_len = 500];
  bool include_deleted = 4;
  
  // Sorting
  message OrderBy {
    string field = 1;
    bool descending = 2;
  }
  repeated OrderBy order_by = 5;
}

message ListUsersResponse {
  repeated User users = 1;
  string next_page_token = 2;
  int32 total_size = 3;
}

message CreateUserRequest {
  string email = 1 [(validate.rules).string.email = true];
  string name = 2 [(validate.rules).string.min_len = 1];
  UserRole role = 3;
  map<string, string> metadata = 4;
}

message CreateUsersResponse {
  message CreateResult {
    User user = 1;
    string error = 2;
  }
  repeated CreateResult results = 1;
  int32 success_count = 2;
  int32 failure_count = 3;
}

message BatchGetUsersRequest {
  repeated string ids = 1 [(validate.rules).repeated.max_items = 100];
}

message BatchGetUsersResponse {
  map<string, User> users = 1;
  repeated string not_found = 2;
}

message StreamUserUpdatesRequest {
  repeated string user_ids = 1;
}
```

### 3.2 gRPC Streaming Patterns

```python
# Server streaming: GetUserOrders
async def stream_user_orders(request, context):
    """Stream orders for a user."""
    user_id = request.user_id
    
    async for order in order_service.stream_orders(user_id):
        yield order
        # Check for cancellation
        if context.cancelled():
            return

# Client streaming: CreateUsers
async def create_users(stub, user_requests):
    """Send multiple user creation requests."""
    async def request_generator():
        for user_data in user_requests:
            yield user_data
            # Simulate delay between requests
            await asyncio.sleep(0.1)
    
    response = await stub.CreateUsers(request_generator())
    return response

# Bidirectional streaming: StreamUserUpdates
async def stream_user_updates(stub, user_ids):
    """Real-time user update stream with subscription management."""
    async def request_generator():
        for user_id in user_ids:
            yield StreamUserUpdatesRequest(user_id=user_id)
            await asyncio.sleep(30)  # Heartbeat
    
    responses = stub.StreamUserUpdates(request_generator())
    
    async for response in responses:
        if response.HasField('update'):
            print(f"User update: {response.update}")
        elif response.HasField('delete'):
            print(f"User deleted: {response.delete}")
```

### 3.3 gRPC Error Handling

```python
from grpc import StatusCode
from grpc StatusError

class GrpcError(Exception):
    def __init__(self, code, message, details=None):
        self.code = code
        self.message = message
        self.details = details or {}

# Server-side error raising
async def get_user(request, context):
    user = await user_service.get_user(request.id)
    
    if not user:
        context.abort(
            StatusCode.NOT_FOUND,
            f"User {request.id} not found"
        )
    
    if not user.active:
        context.abort(
            StatusCode.FAILED_PRECONDITION,
            "User account is inactive",
            details=[{"type": "user_inactive", "user_id": request.id}]
        )
    
    return user

# Client-side error handling
try:
    response = await stub.GetUser(request)
except grpc.RpcError as e:
    if e.code() == StatusCode.NOT_FOUND:
        logger.warning(f"User not found: {e.details()}")
    elif e.code() == StatusCode.UNAUTHENTICATED:
        # Re-authenticate and retry
        await refresh_token()
        response = await stub.GetUser(request)
    elif e.code() == StatusCode.DEADLINE_EXCEEDED:
        logger.error(f"Request timed out: {e.details()}")
    else:
        raise
```

---

## 4. API Versioning

### 4.1 Versioning Strategies

```yaml
# Strategy 1: URL Path Versioning (Most common)
GET /v1/users
GET /v2/users

# Pros: Easy to route, visible in logs
# Cons: URL changes, more complex routing

# Strategy 2: Header Versioning
GET /users
Accept: application/vnd.example.v2+json
API-Version: 2024-01-01

# Pros: Clean URLs
# Cons: Hidden, harder to test

# Strategy 3: Query Parameter
GET /users?version=2

# Pros: Easy to add
# Cons: Clutters URLs, caching issues

# Recommended: URL Path + Header for deprecation
# URL for routing, Header for fine-grained control
```

### 4.2 Deprecation Policy

```yaml
# Minimum version support: 2 versions active
# Deprecation timeline:
# - Announce deprecation: 6 months before sunset
# - Maintain old version: Minimum 12 months
# - Sunset old version: After new version stable

# Deprecation headers:
Deprecation: true
Sunset: Sat, 31 Dec 2024 23:59:59 GMT
Link: <https://api.example.com/docs/v2>; rel="deprecation"; type="text/html"
X-API-Deprecated: true
X-API-Sunset-Date: 2024-12-31

# Error response for deprecated API:
{
  "error": {
    "code": "DEPRECATED_VERSION",
    "message": "API version v1 is deprecated",
    "details": {
      "sunset_date": "2024-12-31",
      "migration_guide": "https://api.example.com/docs/migration/v1-to-v2"
    }
  }
}
```

---

## 5. Authentication & Authorization Headers

### 5.1 Standard Auth Headers

```http
# Bearer Token (JWT, OAuth)
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...

# Basic Auth (rarely used for APIs)
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

# API Key
X-API-Key: sk_live_abc123def456
# OR
Authorization: ApiKey sk_live_abc123def456

# Mutual TLS (no header, uses client cert)
```

### 5.2 Custom Headers Convention

```http
# Request tracing
X-Request-ID: f47ac10b-58cc-4372-a567-0e02b2c3d479
X-Correlation-ID: abc123
X-Forwarded-For: 203.0.113.195, 70.41.3.18, 150.172.238.178
X-Real-IP: 203.0.113.195

# Feature flags / context
X-Tenant-ID: tenant_abc123
X-Feature-Dark-Mode: true
X-Preferred-Language: en-US

# Rate limiting (response)
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 999
X-RateLimit-Reset: 1706703600
Retry-After: 60

# Pagination
X-Total-Count: 1500
X-Page-Limit: 20
X-Page-Offset: 0
```

---

## 6. CORS Configuration

### 6.1 CORS Headers

```http
# Response headers for CORS
Access-Control-Allow-Origin: https://app.example.com
# OR for multiple origins (must validate in application):
Access-Control-Allow-Origin: https://app.example.com

Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization, X-Request-ID, X-Correlation-ID
Access-Control-Expose-Headers: X-Request-ID, X-RateLimit-*
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 86400  # 24 hours, cache preflight

# Preflight request (OPTIONS)
OPTIONS /v1/users HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type, Authorization
```

---

## 7. API Security Checklist

```yaml
# Authentication
- [ ] Require authentication for all non-public endpoints
- [ ] Validate tokens on every request
- [ ] Use short-lived access tokens (15-60 min)
- [ ] Implement refresh token rotation
- [ ] Support API key rotation

# Authorization
- [ ] Check permissions on every request
- [ ] Use least-privilege scopes
- [ ] Implement resource-level access control
- [ ] Log all authorization failures

# Input Validation
- [ ] Validate request body against schema
- [ ] Sanitize all string inputs
- [ ] Limit request body size
- [ ] Validate content-type header
- [ ] Check for SQL injection in query params

# Rate Limiting
- [ ] Implement per-user rate limits
- [ ] Implement per-IP rate limits for unauthenticated
- [ ] Return 429 with Retry-After header
- [ ] Consider burst limits

# Security Headers
- [ ] Content-Security-Policy (if serving HTML)
- [ ] X-Content-Type-Options: nosniff
- [ ] X-Frame-Options: DENY
- [ ] Strict-Transport-Security (HSTS)
- [ ] X-XSS-Protection (legacy browsers)

# Logging & Monitoring
- [ ] Log all authentication failures
- [ ] Log all authorization failures
- [ ] Log suspicious activity (unusual patterns)
- [ ] Alert on rate limit hits
- [ ] Alert on error rate spikes
```

---

## 8. API Design Anti-Patterns

```yaml
# ❌ Chasing the own tail (circular dependency)
# API calls itself through an alias
# User A -> /users -> /users
GET /users
Response: { "aliases": ["/users"] }

# ❌ Random batching
# Batch endpoint that does unrelated operations
POST /api/batch
Body: { "operations": [
    { "op": "get_user", "id": "123" },
    { "op": "delete_order", "id": "456" }
  ]}
# Should be separate calls or use GraphQL

# ❌ Version in body
POST /api/users
Body: { "version": "2.0", "data": {...} }

# ❌ Wrong HTTP status codes
# 200 for errors
# 500 for validation errors
# 404 for authorization (should be 403)

# ❌ Nested resources too deep
# Bad: /orgs/{org}/teams/{team}/members/{member}/roles/{role}
# Better: /members/{member}?include=roles

# ❌ Inconsistent naming
# /getUser, /list_users, /fetchUserOrders, /userList
# Should all use same convention: GET /users, GET /users/{id}, GET /users/{id}/orders

# ❌ Sensitive data in URLs or logs
# GET /users/123?token=xyz
# Authorization header is better (not logged by default)

# ❌ No pagination on large collections
# Returning 100,000 users in one response
# Must implement pagination
```

---

## Links

### Core Router
- `core/DECAPOD.md` - **Router and navigation charter (START HERE)**
- `core/ENGINEERING_EXCELLENCE.md` - Engineering standards

### Architecture (This Section)
- `architecture/WEB.md` - Web API patterns
- `architecture/AUTH.md` - Authentication patterns
- `architecture/MESSAGING.md` - Async API patterns
- `architecture/KUBERNETES.md` - API gateway in K8s

### Authority (Constitution Layer)
- `specs/INTENT.md` - **Methodology contract (READ FIRST)**
- `specs/SYSTEM.md` - System definition and authority doctrine
- `specs/SECURITY.md` - Security doctrine

### Interface Contracts
- `interfaces/CLAIMS.md` - Promises ledger
- `interfaces/CONTROL_PLANE.md` - Agent sequencing patterns
- `interfaces/DOC_RULES.md` - Doc compilation rules

### Methodology
- `methodology/ARCHITECTURE.md` - Architecture decision methodology
- `methodology/TESTING.md` - API testing methodology

---

## Version History

When agents design APIs:
1. Follow existing patterns in the codebase
2. Document all endpoints
3. Include OpenAPI specs in PR
4. Add integration tests for critical paths

---

## Links

### Related Architecture
- [WEB]WEB.md - Web architecture
- [SECURITY]SECURITY.md - API security
- [GRAPHQL]GRAPHQL.md - GraphQL patterns
- [GRPC]GRPC.md - gRPC patterns

### Parent Docs
- [DECAPOD]../core/DECAPOD.md - Router and navigation charter
- [INTERFACES]../core/INTERFACES.md - Interface contracts
- [INTENT]../specs/INTENT.md - Intent specification

| Version | Date | Changes |
|---------|------|---------|
| 1.0 | 2024-01-15 | Expanded comprehensive API design reference |