Created
March 14, 2026 15:12
-
-
Save mohashari/bc69361c5aff9beb588ae4d5c0956ad0 to your computer and use it in GitHub Desktop.
Code snippets — Rest Api Design Principles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "data": [...], | |
| "meta": { | |
| "total": 1543, | |
| "page": 2, | |
| "per_page": 20, | |
| "next_cursor": "eyJpZCI6MTIwfQ" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /users # collection | |
| /users/42 # single resource | |
| /users/42/posts # nested resource |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 200 OK — Success, returning data | |
| 201 Created — Resource created (POST) | |
| 204 No Content — Success, nothing to return (DELETE) | |
| 400 Bad Request — Client sent invalid data | |
| 401 Unauthorized — Authentication required | |
| 403 Forbidden — Authenticated but not authorized | |
| 404 Not Found — Resource doesn't exist | |
| 409 Conflict — State conflict (duplicate, etc.) | |
| 422 Unprocessable Entity — Validation failed | |
| 429 Too Many Requests — Rate limited | |
| 500 Internal Server Error — Your bug |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /api/v1/users | |
| /api/v2/users |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Accept: application/vnd.myapp.v2+json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Offset pagination | |
| GET /users?page=2&per_page=20 | |
| # Cursor pagination (better for large datasets) | |
| GET /users?cursor=eyJpZCI6MTAwfQ&limit=20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| GET /users?status=active&role=admin&sort=created_at&order=desc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Content-Type: application/json | |
| Accept: application/json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| POST /payments | |
| Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000 | |
| { | |
| "amount": 9900, | |
| "currency": "USD" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "error": { | |
| "code": "VALIDATION_FAILED", | |
| "message": "Request validation failed", | |
| "details": [ | |
| { | |
| "field": "email", | |
| "message": "Must be a valid email address" | |
| } | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Bad | |
| GET /getUsers | |
| POST /createUser | |
| DELETE /deleteUser/42 | |
| # Good | |
| GET /users | |
| POST /users | |
| DELETE /users/42 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| openapi: 3.1.0 | |
| info: | |
| title: My API | |
| version: 1.0.0 | |
| paths: | |
| /users: | |
| get: | |
| summary: List all users | |
| parameters: | |
| - name: page | |
| in: query | |
| schema: | |
| type: integer | |
| responses: | |
| '200': | |
| description: Success | |
| content: | |
| application/json: | |
| schema: | |
| $ref: '#/components/schemas/UserList' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment