Introduction
Building my first Login API using Python and Flask was a great hands-on experience. Through this task, I learned how a backend API receives a request, validates user input, verifies credentials, and returns an appropriate response.
In this blog, I’m sharing the complete process of how I built and tested the Login API.
Project Goal
The goal of this project was to build a simple Login API that can:
• Accept email and password
• Validate the request
• Verify user credentials
• Return JSON responses
• Handle invalid requests
• Use appropriate HTTP status codes
• Test different API scenarios
Technologies I Used
• Python
• Flask
• REST API
• JSON
• Thunder Client
• GitHub
Project Structure
I organized the project with a simple and clean structure:
login-api/
│
├── app.py
├── requirements.txt
├── README.md
│
├── routes/
│ ├── init.py
│ └── auth.py
│
└── services/
├── init.py
└── user_service.py
Separating the routes and service logic makes the project easier to understand and maintain.
Step 1:Creating the Flask Application
I created app.py as the main entry point of the application.
The Flask application initializes the server and registers the authentication route.
After running the application, the API was available locally at:
http://127.0.0.1:5000
Step 2:Creating the Login Endpoint
I created a Login API using the POST method.
Endpoint:
POST /login
The API accepts email and password as JSON data.
Example request:
{
"email": "admin@gmail.com",
"password": "admin123"
}
Step 3:Request Validation
Before checking the credentials, the API validates the incoming request.
For example, when an empty request body is sent:
{}
The API returns:
{
"success": false,
"message": "Request body is required"
}
If the email or password is missing:
{
"success": false,
"message": "Email and password are required"
}
This helped me understand the importance of validating user input before processing a request.
Step 4: Credential Validation
I created a separate service layer in user_service.py to validate the login credentials.
The service checks whether the provided email and password match the configured credentials.
This separation helped me understand how API routes and business logic can be organized independently.
Step 5: Successful Login
When the correct credentials are provided, the API returns:
{
"success": true,
"message": "Login successful",
"user": {
"email": "admin@gmail.com"
}
}
The API returns HTTP 200, indicating that the login was successful.
Step 6: Invalid Credentials
When incorrect credentials are provided, the API returns:
{
"success": false,
"message": "Invalid email or password"
}
The API returns HTTP 401 for invalid credentials.
Step 7: Testing the API
After completing the API, I tested it using Thunder Client.
I tested different scenarios:
Test Scenario Result
Valid email and password ✅ Login successful
Empty request body ✅ Validation handled
Missing email/password ✅ Validation handled
Invalid credentials ✅ Error handled
Testing multiple scenarios helped me make sure that the API was not only working for successful login but was also handling invalid inputs correctly.
Step 8: Documentation
I created a README.md file to document the project.
The documentation includes:
• Project overview
• Features
• API endpoint
• Request body
• Response examples
• Technologies used
Complete Login API Flow
The complete flow of my API is:
Client
↓
POST /login
↓
Request Validation
↓
Email & Password Validation
↓
Credential Verification
↓
Success / Error Response
What I Learned
This project helped me understand the basic workflow of backend API development.
I learned how to:
• Build a REST API using Flask
• Handle POST requests
• Work with JSON data
• Validate user input
• Handle authentication logic
• Use HTTP status codes
• Test APIs using Thunder Client
• Structure a Flask project
• Document and maintain a project using GitHub
Final Thought
This project was a valuable hands-on experience for me because I was able to understand the complete flow of building and testing an API rather than just writing individual pieces of code.
It also helped me understand that a good API is not only about functionality. Validation, error handling, testing, documentation, and clean project structure are equally important.
I'm excited to continue learning Python, Flask, and backend development by building more real-world projects.
Top comments (0)