Skip to content

Commit fa84097

Browse files
authored
Merge pull request #221 from codeharborhub/dev-1
Add-docs: For api's design
2 parents 7344a19 + 5553507 commit fa84097

File tree

9 files changed

+699
-0
lines changed

9 files changed

+699
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "API Design & REST",
3+
"position": 8,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn how to build the bridges that connect your server to the world. Master RESTful principles, HTTP methods, and status codes."
7+
}
8+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
sidebar_position: 8
3+
title: "API Testing Tools"
4+
sidebar_label: "8. Testing Tools"
5+
description: "Learn how to test and debug your API endpoints using Postman, Thunder Client, and cURL."
6+
---
7+
8+
You’ve designed your endpoints, chosen your status codes, and secured them with JWT. Now, how do you actually "call" them? Since we don't have a frontend yet, we use **API Testing Tools**.
9+
10+
These tools act as a "Universal Client" that can send any HTTP request to any server.
11+
12+
## Top 3 Tools for Developers
13+
14+
<Tabs>
15+
<TabItem value="thunder" label="⚡ Thunder Client (Recommended)" default>
16+
17+
### Why we love it:
18+
It is a lightweight extension that lives inside **VS Code**. You don't need to switch between apps!
19+
20+
* **Best For:** Quick testing during coding.
21+
* **Pros:** Fast, stays in your editor, supports "Collections" and "Environment Variables."
22+
* **Installation:** Search for "Thunder Client" in the VS Code Extensions marketplace.
23+
24+
</TabItem>
25+
<TabItem value="postman" label="🚀 Postman">
26+
27+
### Why we love it:
28+
The industry leader. If you work at a big tech company, they probably use Postman to document and share APIs.
29+
30+
* **Best For:** Complex projects and team collaboration.
31+
* **Pros:** Advanced automated testing, beautiful documentation, and cloud sync.
32+
33+
</TabItem>
34+
<TabItem value="curl" label="💻 cURL">
35+
36+
### Why we love it:
37+
A command-line tool that comes pre-installed on almost every OS (Linux, Mac, Windows).
38+
39+
* **Best For:** Quick checks and automation scripts.
40+
* **Example:**
41+
```bash
42+
curl -X GET https://api.codeharborhub.com/v1/users
43+
```
44+
45+
</TabItem>
46+
</Tabs>
47+
48+
## Anatomy of an API Request
49+
50+
When using these tools, you will need to configure four main parts:
51+
52+
1. **Method:** Select GET, POST, PUT, or DELETE.
53+
2. **URL (Endpoint):** Where is your server running? (e.g., `http://localhost:5000/api/users`).
54+
3. **Headers:** This is where you put your **Content-Type** (usually `application/json`) and your **Authorization** tokens.
55+
4. **Body:** For POST and PUT requests, this is where you paste your JSON data.
56+
57+
## Understanding the Response
58+
59+
Once you hit "Send," the tool will show you:
60+
61+
```mermaid
62+
graph TD
63+
A[Hit Send] --> B{Server Process}
64+
B --> C[Status Code: e.g., 200 OK]
65+
B --> D[Response Time: e.g., 150ms]
66+
B --> E[Response Body: The JSON Data]
67+
68+
```
69+
70+
## Professional Workflow: Environment Variables
71+
72+
Don't hardcode your URLs! Imagine you have 50 requests. If you change your port from `5000` to `8000`, you don't want to edit 50 requests manually.
73+
74+
**Use Variables:**
75+
76+
* Create a variable called `{{base_url}}`.
77+
* Set it to `http://localhost:5000`.
78+
* Your requests will look like: `{{base_url}}/users`.
79+
80+
*Now, when you go to production, you just change the variable once!*
81+
82+
## Summary Checklist
83+
84+
* [x] I have installed **Thunder Client** or **Postman**.
85+
* [x] I know how to set the HTTP Method and URL.
86+
* [x] I can send a JSON body in a POST request.
87+
* [x] I understand how to read the Status Code and Response Body.
88+
89+
:::success 🎉 Module Complete!
90+
You have mastered the theory of **Relational Databases** and **API Design**. You are now officially ready to start writing real code!
91+
:::
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
sidebar_position: 6
3+
title: "API Authentication"
4+
sidebar_label: "6. Authentication"
5+
description: "Learn how to secure your APIs using modern authentication methods like API Keys and JWT."
6+
---
7+
8+
In the previous lessons, we built APIs that anyone could call. But in the real world, you don't want strangers deleting your data! **Authentication (AuthN)** is how the server verifies that a request is coming from a valid user.
9+
10+
## 🧐 Authentication vs. Authorization
11+
12+
These two sound similar, but they are very different. At **CodeHarborHub**, we use the "Office Building" analogy:
13+
14+
* **Authentication (AuthN):** Showing your ID card at the gate to enter the building. (*Who are you?*)
15+
* **Authorization (AuthZ):** Your ID card only lets you into the 4th floor, not the CEO's office. (*What are you allowed to do?*)
16+
17+
## Common Auth Methods
18+
19+
Modern APIs usually use one of these three methods to keep things secure:
20+
21+
<Tabs>
22+
<TabItem value="apikey" label="🔑 API Keys" default>
23+
24+
### Simple & Fast
25+
The server gives the client a long, secret string (the Key). The client sends this key in the header of every request.
26+
* **Best For:** Public APIs (like Google Maps or Weather APIs).
27+
* **Risk:** If someone steals your key, they can act as you.
28+
29+
</TabItem>
30+
<TabItem value="jwt" label="🎟️ JWT (Tokens)">
31+
32+
### The Industry Standard
33+
**JSON Web Tokens** are like a digital "Boarding Pass." Once you login, the server gives you a signed token. You show this token for every future request.
34+
* **Best For:** Modern Web and Mobile apps.
35+
* **Feature:** It's **Stateless** (the server doesn't need to check the database every time).
36+
37+
</TabItem>
38+
<TabItem value="oauth" label="🤝 OAuth 2.0">
39+
40+
### Third-Party Login
41+
This allows you to "Login with Google" or "Login with GitHub."
42+
* **Best For:** User convenience and high security.
43+
* **Benefit:** You don't have to manage passwords; Google does it for you!
44+
45+
</TabItem>
46+
</Tabs>
47+
48+
## The Token-Based Workflow (JWT)
49+
50+
This is the most common flow you will build as a Backend Developer:
51+
52+
```mermaid
53+
sequenceDiagram
54+
participant C as Client (User)
55+
participant S as Server (API)
56+
participant DB as Database
57+
58+
C->>S: 1. Login (Username + Password)
59+
S->>DB: 2. Verify Credentials
60+
DB-->>S: 3. Correct!
61+
S->>S: 4. Generate JWT Token
62+
S-->>C: 5. Send Token back
63+
Note over C,S: Subsequent Requests
64+
C->>S: 6. GET /profile (Header: Bearer <Token>)
65+
S->>S: 7. Verify Token Signature
66+
S-->>C: 8. Here is your Private Data!
67+
68+
```
69+
70+
## Best Practices for API Security
71+
72+
1. **Always use HTTPS:** Never send passwords or tokens over `http`. They can be easily stolen.
73+
2. **Use the Authorization Header:** Don't put tokens in the URL. Use the standard header:
74+
`Authorization: Bearer <your_token_here>`
75+
3. **Set Expiration:** Tokens should not last forever. If a token is stolen, it should expire in a few hours.
76+
4. **Don't Store Secrets in Frontend:** Never hardcode your API keys in your React or HTML code. Use `.env` files.
77+
78+
## Summary Checklist
79+
80+
* [x] I understand that Authentication proves **identity**.
81+
* [x] I know the difference between Authentication and Authorization.
82+
* [x] I understand the JWT "Boarding Pass" workflow.
83+
* [x] I know that sensitive data must always be sent over HTTPS.
84+
85+
:::danger Security Warning
86+
Never, ever commit your API keys or Secrets to **GitHub**! If you do, hackers can find them in seconds. Always use a `.gitignore` file to hide your environment variables.
87+
:::
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
sidebar_position: 3
3+
title: "HTTP Methods & Status Codes"
4+
sidebar_label: "3. Methods & Status"
5+
description: "Learn the vocabulary of the web—how to tell the server what to do and how to understand its response."
6+
---
7+
8+
When you send a request to a REST API, you are essentially sending a message. To make sure the server understands you, you must use specific **Methods**. Once the server processes your request, it replies with a **Status Code**.
9+
10+
## HTTP Methods (The "Verbs")
11+
12+
Every request starts with a method that defines the **intent** of the action.
13+
14+
<Tabs>
15+
<TabItem value="get" label="📖 GET" default>
16+
17+
### Purpose: Retrieve Data
18+
Used when you want to "fetch" information. It is **Read-Only**.
19+
* **Analogy:** Browsing a menu at a cafe.
20+
* **Example:** `GET /users` (Get all users).
21+
22+
</TabItem>
23+
<TabItem value="post" label="➕ POST">
24+
25+
### Purpose: Create Data
26+
Used to send new data to the server to create a resource.
27+
* **Analogy:** Placing a new order at the counter.
28+
* **Example:** `POST /users` (Create a new account).
29+
30+
</TabItem>
31+
<TabItem value="put" label="📝 PUT / PATCH">
32+
33+
### Purpose: Update Data
34+
* **PUT:** Replaces the entire resource.
35+
* **PATCH:** Updates only specific parts (like just the email).
36+
* **Analogy:** Changing your mobile number on an existing account.
37+
38+
</TabItem>
39+
<TabItem value="delete" label="🗑️ DELETE">
40+
41+
### Purpose: Remove Data
42+
Used to delete a specific resource.
43+
* **Analogy:** Canceling your subscription.
44+
* **Example:** `DELETE /users/1` (Delete user with ID 1).
45+
46+
</TabItem>
47+
</Tabs>
48+
49+
## HTTP Status Codes (The Feedback)
50+
51+
After you send a request, the server sends back a 3-digit number. This tells the client exactly what happened.
52+
53+
| Range | Category | "Desi" Meaning | Typical Examples |
54+
| :--- | :--- | :--- | :--- |
55+
| **2xx** | **Success** | "Sab sahi hai!" (All Good) | **200 OK**, **201 Created** |
56+
| **3xx** | **Redirection** | "Rasta badal lo." (Go elsewhere) | **301 Moved Permanently** |
57+
| **4xx** | **Client Error** | "Tumhari galti hai." (Your fault) | **400 Bad Request**, **404 Not Found** |
58+
| **5xx** | **Server Error** | "Meri galti hai." (My fault) | **500 Internal Server Error** |
59+
60+
## Understanding the "Big Three" Codes
61+
62+
1. **200 OK:** The request was successful, and here is your data!
63+
2. **404 Not Found:** The URL you requested doesn't exist. (The "classic" internet error).
64+
3. **500 Internal Server Error:** Your code crashed on the server. The client did nothing wrong, but the backend failed.
65+
66+
## Summary Checklist
67+
* [x] I know that **GET** is for reading and **POST** is for creating.
68+
* [x] I understand the difference between **PUT** (Full update) and **PATCH** (Partial).
69+
* [x] I can identify that **4xx** codes mean the issue is on the user's side.
70+
* [x] I know that **201** is the standard code for a successful "Create" action.
71+
72+
:::warning Common Beginner Mistake
73+
Never use **GET** to send sensitive data like passwords! Data in a GET request is visible in the URL. Always use **POST** with a "Body" for secure information.
74+
:::
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
sidebar_position: 5
3+
title: "JSON & API Communication"
4+
sidebar_label: "5. JSON APIs"
5+
description: "Learn about JSON, the universal data format for modern API communication."
6+
---
7+
8+
When a Waiter (the API) brings your order from the Kitchen (the Server), the food is served on a **Plate**. In the digital world, **JSON** is that plate.
9+
10+
**JSON** stands for **JavaScript Object Notation**. It is a lightweight, text-based format that is easy for humans to read and even easier for machines to parse.
11+
12+
## 🧐 Why JSON?
13+
14+
Before JSON, the world used **XML**, which was very bulky and hard to read. JSON won the "format war" because:
15+
1. **Lightweight:** It uses very few characters, making it fast to send over the internet.
16+
2. **Language Independent:** Even though it has "JavaScript" in the name, almost every language (Python, Java, PHP, C#) can read it.
17+
3. **Key-Value Pairs:** It organizes data exactly like a dictionary or a map.
18+
19+
## The Structure of a JSON Object
20+
21+
A JSON object is wrapped in curly braces `{}` and contains data in `key: value` pairs.
22+
23+
```json
24+
{
25+
"id": 101,
26+
"name": "Ajay Dhangar",
27+
"isMentor": true,
28+
"skills": ["React", "Node.js", "SQL"],
29+
"address": {
30+
"city": "Mandsaur",
31+
"state": "MP"
32+
}
33+
}
34+
```
35+
36+
### Supported Data Types
37+
38+
<Tabs>
39+
<TabItem value="strings" label="🔤 Strings" default>
40+
Text wrapped in double quotes: `"Hello CodeHarborHub"`
41+
</TabItem>
42+
<TabItem value="numbers" label="🔢 Numbers">
43+
Integers or decimals: `25` or `99.99`
44+
</TabItem>
45+
<TabItem value="arrays" label="📚 Arrays">
46+
Ordered lists inside brackets: `["JavaScript", "HTML"]`
47+
</TabItem>
48+
<TabItem value="objects" label="📂 Objects">
49+
Nested data inside braces: `{"key": "value"}`
50+
</TabItem>
51+
</Tabs>
52+
53+
## Sending and Receiving JSON
54+
55+
When you build a backend at **CodeHarborHub**, you will deal with JSON in two ways:
56+
57+
### 1. The Request Body
58+
59+
When a user registers, they send their data to your server as a JSON string.
60+
61+
```http
62+
POST /users
63+
Content-Type: application/json
64+
65+
{
66+
"username": "coder_ajay",
67+
"password": "securepassword123"
68+
}
69+
```
70+
71+
### 2. The Response
72+
73+
Your server processes the logic and sends back a JSON response.
74+
75+
```json
76+
{
77+
"status": "success",
78+
"message": "User created successfully",
79+
"userId": 502
80+
}
81+
```
82+
83+
## Tools for JSON
84+
85+
### 1. JSON Formatter
86+
87+
Raw JSON can sometimes look like a giant "wall of text." Use a browser extension like **JSON Viewer** to make it look beautiful and organized.
88+
89+
### 2. JSON.stringify() vs JSON.parse()
90+
91+
If you are using JavaScript:
92+
93+
* **`JSON.stringify(obj)`**: Converts a JS object into a JSON string (to send to a server).
94+
* **`JSON.parse(str)`**: Converts a JSON string into a JS object (to use in your code).
95+
96+
## Summary Checklist
97+
98+
* [x] I understand that JSON is a text format for storing and transporting data.
99+
* [x] I know that JSON uses Key-Value pairs.
100+
* [x] I can identify different data types like Strings, Numbers, and Arrays in JSON.
101+
* [x] I understand that `Content-Type: application/json` is required in the header.
102+
103+
:::tip Pro-Tip
104+
Always validate your JSON! A single missing comma `,` or a missing double quote `"` will cause your entire API request to fail. Use a tool like **JSONLint** to check your work.
105+
:::

0 commit comments

Comments
 (0)