DEV Community

Cover image for My Journey Building a Dynamic Profile API with FastAPI
Ukeme Edet
Ukeme Edet

Posted on

My Journey Building a Dynamic Profile API with FastAPI

I'm excited to share my latest project: a simple but powerful RESTful API that serves up my professional profile and a random cat fact with every request! This task was a great way to practice consuming third-party APIs, handling dynamic data, and structuring a clean JSON response.

The Challenge

The goal was to create a /me endpoint that returns a JSON object containing:

  • My name, email, and tech stack
  • The current UTC time in ISO 8601 format
  • A fresh cat fact from the Cat Facts API (https://catfact.ninja/fact)

My Approach

I chose the Python/FastAPI stack for its speed, simplicity, and modern features. Here’s a breakdown of my process:

  1. Project Setup: I started by setting up a clean project structure with main.py for the application logic, requirements.txt for dependencies, and a README.md for documentation.

  2. FastAPI and Pydantic: I used FastAPI to define the /me endpoint and Pydantic to create models for the response. This ensures the data is always in the correct format.

  3. Fetching Cat Facts: The httpx library made it easy to make an asynchronous request to the Cat Facts API. I also implemented error handling to gracefully manage situations where the external API might be down.

  4. Dynamic Timestamp: I used Python's datetime module to generate the current UTC time for each request, ensuring the timestamp is always up-to-date.

  5. CORS: I enabled CORS (Cross-Origin Resource Sharing) to allow the API to be accessed from any web browser.

What I Learned

  • API Integration: This project was a great reminder of how to effectively consume external APIs and handle potential failures.
  • Data Modelling: Pydantic is a powerful tool for data validation and ensuring a consistent API response.
  • Asynchronous Programming: Using async and await with httpx is a clean and efficient way to handle HTTP requests in FastAPI.
  • Best Practices: From project structure to error handling, this task reinforced the importance of writing clean, maintainable code.

Check out the running application and the code on my GitHub!

#FastAPI #Python #API #Backend #Developer #HNGInternship #Tech

FastAPI - Cat Fact

Top comments (0)