Build One API Endpoint
1. Build One API Endpoint
The database is connected. Now let's build something with it. You'll ask Claude to create a new endpoint in the Music Analytics API that returns albums by artist: real data, real code, one prompt.2. From Query to API
The previous video was exploratory: chatting with the database through Claude. This one is about shipping. Take that same data and expose it through a proper API endpoint, one prompt at a time.3. The Project So Far
A new Blueprint, db_routes.py, gets added to the project. Claude will also wire it into app.py. This keeps database-backed endpoints separate from the existing routes, a clean separation of concerns. The Chinook database is already connected via MCP. The structure is in place; one piece left to add.4. Ask Claude to Build It
Here's the prompt used to build the endpoint. Claude reads the schema through MCP, understands the table relationships, and gets to work. The output shows exactly what it did: created db_routes.py with a database connection helper and the new endpoint, then updated app.py to register the Blueprint. It even added a 404 for missing artists and sorted results by title — details that weren't in the prompt.5. The Generated Code
The new endpoint captures the artist ID from the URL, runs parameterized SQL to prevent injection, and returns clean JSON. The database connection is closed after use. One sentence in, production-ready code out.6. Test It Locally
Let's verify it actually works. The command starts the server in the background, waits three seconds for it to come up, then hits the endpoint with artist ID 1. The pause is there to make sure the server is ready before the request goes out. The response comes back with a 200 — a successful HTTP response — and clean JSON: AC/DC, with "For Those About to Rock We Salute You" and "Let There Be Rock." The endpoint works exactly as requested.7. Handle Edge Cases
There's a gap in the current code. Request artist ID 99999 and the response is an empty array — no error, no indication anything went wrong. API consumers need clear signals when data doesn't exist. That means proper 404 responses and validated response structures.8. Add Validation with Pydantic
Pydantic solves this cleanly. Define response models: Album with id and title, ArtistAlbums wrapping the list. This gives type validation, automatic serialization, and clear errors if the data doesn't match. It also makes the API self-documenting.9. Ask Claude to Add Validation
One follow-up prompt: add Pydantic validation and return 404 if the artist doesn't exist or has no albums. Claude updates the endpoint with the models and proper status codes. The API now responds correctly to both valid and invalid requests.10. Let's Practice!
One full endpoint: built, tested, and validated with Claude. Now build your own. Describe the data, let Claude implement it. Let's practice.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.