From Zero to FastAPI Hero: My HNG12 Stage 0 Adventure

Hey there, fellow backend wizards and code crusaders š!
Welcome to my behind-the-scenes journey into the HNG12 Stage 0 assignment. Today, Iām taking you on a rollercoaster ride through the creation of a sleek FastAPI application that does three cool things:
Returns my registered HNG email,
Shows the current UTC datetime in ISO 8601 format, and
Shares the GitHub URL of this awesome project!
Grab your favorite beverage, and letās dive in!
The Mission: What Was I Asked to Do?
The challenge was crystal clear: build a public API that serves up a neat JSON bundle containing:
Email: (Yep, my registered HNG email!)
Current Datetime: Freshly minted in UTC, right when you ask.
GitHub URL: A link to where all the magic happens (i.e., the project repo).
I opted for Python and FastAPI because they make creating and deploying APIs feel like a walk in the park (with a few unexpected loop-de-loops for fun).
Rolling Up My Sleeves: Setting Up the API
I started by creating a virtual environment (because who doesnāt love a good sandbox to play in?), then installed FastAPI and Uvicorn. With my coding hat on and coffee in hand, I crafted the following masterpiece:
from fastapi import FastAPI # Import our trusty web framework
from fastapi.middleware.cors import CORSMiddleware # For smooth cross-origin magic
from datetime import datetime, timezone # To grab the current time in UTC
app = FastAPI() # Creating our FastAPI app instance
# Adding CORS middleware to handle all those cross-origin requests
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Open to all domains (tweak for production if needed)
allow_credentials=True, # Cookies and authorization headers? Yes, please!
allow_methods=["GET"], # We only need GET since we're all about simple retrieval
allow_headers=["*"], # Let every header join the party
)
@app.get("/") # The root endpoint of our API
def get_info():
return {
"email": "joseph.adamu.it@gmail.com", # My registered HNG email
"current_datetime": datetime.now(timezone.utc).isoformat(), # Fresh UTC datetime
"github_url": "https://github.com/Joe-encodes/fastapi-project" # Link to my GitHub repo
}
if __name__ == "__main__": # Only run this if the script is executed directly
import uvicorn # Uvicorn: the lightweight Python web server that brings our API to life
uvicorn.run(app, host="0.0.0.0", port=8000)
Whatās happening here?
FastAPI is our magic wand that turns code into a live API.
CORS middleware ensures that our API is friendly and accessible from any domain (because sharing is caring).
The
get_infoendpoint does all the heavy lifting by packaging my email, current UTC time, and GitHub link into a neat JSON gift.
Testing the Waters
After coding, I fired up the API locally with Uvicorn and tested it on my browser and with curl. Watching that JSON response pop up was like seeing a well-cooked dish come out of the ovenāāāsatisfying and oh-so-rewarding!
uvicorn main:app --reload
Open your browser to http://127.0.0.1:8000 and feast your eyes on the magic.
Deployment: Taking It Live on Render
Once I confirmed everything was working perfectly on my local machine, I pushed the code to GitHub and connected the repository to Render. With a couple of settings tweaked:
Build Command:
pip install -r requirements.txtStart Command:
uvicorn main:app --host 0.0.0.0 --port 10000
ā¦and voilĆ ! The API was deployed and accessible to the world.
Lessons, Laughter, and Learning
Hereās what I learned on this wild ride:
FastAPI is a blast: Itās fast, fun, and super friendly for developers.
CORS is crucial: Handling cross-origin requests ensures that your API is as open and accessible as you intend it to be.
Deployment can be thrilling: Seeing your code go live is like watching your kid ride a bike for the first timeāāāexhilarating and nerve-wracking all at once.
Wrapping Up
This HNG12 Stage 0 assignment wasnāt just about writing codeāāāit was an adventure in problem-solving, deploying, and celebrating every small victory along the way. I hope my journey inspires you to embrace the fun in backend development and push your own limits.
Until next time, keep coding, stay curious, and may your API always return a happy 200 OK!
Connect With Me
GitHub: Joe-encodes
LinkedIn: Joseph Adamu
Email: joseph.adamu.it@gmail.com
Happy coding, everyone!