From Zero to FastAPI Hero: My Backend 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
- 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:
<span>from</span> <span>fastapi</span> <span>import</span> <span>FastAPI</span> <span># Import our trusty web framework </span><span>from</span> <span>fastapi.middleware.cors</span> <span>import</span> <span>CORSMiddleware</span> <span># For smooth cross-origin magic </span><span>from</span> <span>datetime</span> <span>import</span> <span>datetime</span><span>,</span> <span>timezone</span> <span># To grab the current time in UTC </span><span>app</span> <span>=</span> <span>FastAPI</span><span>()</span> <span># Creating our FastAPI app instance </span><span># Adding CORS middleware to handle all those cross-origin requests </span><span>app</span><span>.</span><span>add_middleware</span><span>(</span><span>CORSMiddleware</span><span>,</span><span>allow_origins</span><span>=</span><span>[</span><span>"</span><span>*</span><span>"</span><span>],</span> <span># Open to all domains (tweak for production if needed) </span> <span>allow_credentials</span><span>=</span><span>True</span><span>,</span> <span># Cookies and authorization headers? Yes, please! </span> <span>allow_methods</span><span>=</span><span>[</span><span>"</span><span>GET</span><span>"</span><span>],</span> <span># We only need GET since we're all about simple retrieval </span> <span>allow_headers</span><span>=</span><span>[</span><span>"</span><span>*</span><span>"</span><span>],</span> <span># Let every header join the party </span><span>)</span><span>@app.get</span><span>(</span><span>"</span><span>/</span><span>"</span><span>)</span> <span># The root endpoint of our API </span><span>def</span> <span>get_info</span><span>():</span><span>return</span> <span>{</span><span>"</span><span>email</span><span>"</span><span>:</span> <span>"</span><span>joseph.adamu.it@gmail.com</span><span>"</span><span>,</span> <span># My registered HNG email </span> <span>"</span><span>current_datetime</span><span>"</span><span>:</span> <span>datetime</span><span>.</span><span>now</span><span>(</span><span>timezone</span><span>.</span><span>utc</span><span>).</span><span>isoformat</span><span>(),</span> <span># Fresh UTC datetime </span> <span>"</span><span>github_url</span><span>"</span><span>:</span> <span>"</span><span>https://github.com/Joe-encodes/fastapi-project</span><span>"</span> <span># Link to my GitHub repo </span> <span>}</span><span>if</span> <span>__name__</span> <span>==</span> <span>"</span><span>__main__</span><span>"</span><span>:</span> <span># Only run this if the script is executed directly </span> <span>import</span> <span>uvicorn</span> <span># Uvicorn: the lightweight Python web server that brings our API to life </span> <span>uvicorn</span><span>.</span><span>run</span><span>(</span><span>app</span><span>,</span> <span>host</span><span>=</span><span>"</span><span>0.0.0.0</span><span>"</span><span>,</span> <span>port</span><span>=</span><span>8000</span><span>)</span><span>from</span> <span>fastapi</span> <span>import</span> <span>FastAPI</span> <span># Import our trusty web framework </span><span>from</span> <span>fastapi.middleware.cors</span> <span>import</span> <span>CORSMiddleware</span> <span># For smooth cross-origin magic </span><span>from</span> <span>datetime</span> <span>import</span> <span>datetime</span><span>,</span> <span>timezone</span> <span># To grab the current time in UTC </span> <span>app</span> <span>=</span> <span>FastAPI</span><span>()</span> <span># Creating our FastAPI app instance </span> <span># Adding CORS middleware to handle all those cross-origin requests </span><span>app</span><span>.</span><span>add_middleware</span><span>(</span> <span>CORSMiddleware</span><span>,</span> <span>allow_origins</span><span>=</span><span>[</span><span>"</span><span>*</span><span>"</span><span>],</span> <span># Open to all domains (tweak for production if needed) </span> <span>allow_credentials</span><span>=</span><span>True</span><span>,</span> <span># Cookies and authorization headers? Yes, please! </span> <span>allow_methods</span><span>=</span><span>[</span><span>"</span><span>GET</span><span>"</span><span>],</span> <span># We only need GET since we're all about simple retrieval </span> <span>allow_headers</span><span>=</span><span>[</span><span>"</span><span>*</span><span>"</span><span>],</span> <span># Let every header join the party </span><span>)</span> <span>@app.get</span><span>(</span><span>"</span><span>/</span><span>"</span><span>)</span> <span># The root endpoint of our API </span><span>def</span> <span>get_info</span><span>():</span> <span>return</span> <span>{</span> <span>"</span><span>email</span><span>"</span><span>:</span> <span>"</span><span>joseph.adamu.it@gmail.com</span><span>"</span><span>,</span> <span># My registered HNG email </span> <span>"</span><span>current_datetime</span><span>"</span><span>:</span> <span>datetime</span><span>.</span><span>now</span><span>(</span><span>timezone</span><span>.</span><span>utc</span><span>).</span><span>isoformat</span><span>(),</span> <span># Fresh UTC datetime </span> <span>"</span><span>github_url</span><span>"</span><span>:</span> <span>"</span><span>https://github.com/Joe-encodes/fastapi-project</span><span>"</span> <span># Link to my GitHub repo </span> <span>}</span> <span>if</span> <span>__name__</span> <span>==</span> <span>"</span><span>__main__</span><span>"</span><span>:</span> <span># Only run this if the script is executed directly </span> <span>import</span> <span>uvicorn</span> <span># Uvicorn: the lightweight Python web server that brings our API to life </span> <span>uvicorn</span><span>.</span><span>run</span><span>(</span><span>app</span><span>,</span> <span>host</span><span>=</span><span>"</span><span>0.0.0.0</span><span>"</span><span>,</span> <span>port</span><span>=</span><span>8000</span><span>)</span>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)
Enter fullscreen mode Exit fullscreen mode
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_info
endpoint 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 <span>--reload</span>uvicorn main:app <span>--reload</span>uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode
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.txt
- Start 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!
暂无评论内容