Getting the Most out of AI Coding in API Design

Getting the Most out of AI Coding in API Design

Posted in

AI coding is bringing us one step closer to one of tech’s fondest wishes. Science fiction writers have been dreaming of the day when computers could produce something out of thin air with just a few words from the user. The ability to generate real, useful, usable products using natural language promises to make technology even more accessible than it already is, even for users with little to no technical experience.

This potential unlocked by this powerful new technology comes with its own potential drawbacks, though. AI coding tools like Cursor or Claude Desktop are powerful, but they’re not infallible. Users with limited technical experience might not realize when code isn’t up to par.

AI coding agents can be used for virtually anything, including API design. This means you could create an API from scratch in an AI coding environment once you know how. To help you get started, we’ve put together some best practices for AI coding in API design.

AI Coding For API Design

AI coding tools can be integrated into the API design process right from the very beginning. Traditionally, the earliest stages of API design involved everyone from product managers to marketing professionals to software engineers. AI coding assistants eliminate all of that, allowing you to spin up a workable API the moment you have a new idea. To help make some of these principles more concrete and practical, we’ll start by showing you how to build a working API using AI coding.

API Coding For API Design: A Case Study

Creating an API with AI coding is as close to plug-and-play as you can get in programming. You can just type “Create a working API” into an AI coding assistant, and it can generate endpoints, sample JSON schemas, and OpenAPI definitions directly from the prompt. Although it’s possible to create a working API with a single query, it’s also quite probable that your API will need some fine-tuning once it’s made. Knowing a thing or two about AI coding and API design will help you get the most out of your AI coding platform.

To illustrate some of these best practices for AI coding, let’s build a working API using Cursor, one of the more popular AI coding assistants. We’ll create a simple API for delivering quotes using a JSON-based dataset of quotes from famous authors from Kaggle. To start, download the dataset and unzip it into a development folder. We’ve labeled ours Quotes, but feel free to choose whatever name you like.

Once the dataset is unzipped, you’re going to open your AI coding assistant. We’ll be using Cursor, but feel free to use whatever AI coding platform you prefer. Once your AI coding platform is open, start by asking the platform to create a simple API that serves a quote from the quotes.JSON file to an endpoint by inputting the following prompt:

Can you create a simple API that serves a quote from `your_data_path` to an endpoint, please?

Remember to input the code path to the quotes.json file from your development directory. Once submitted, the AI coding platform should ask you a follow-up question about what data should be returned. Once you’ve clarified what data you’d like returned, your development server should be served to http://localhost:3000/ using ts-node.

This brings up one potential issue with AI coding platforms. They’ll implement your request in the way it’s most familiar with. If you want to create an API using a different framework, you’ll need to specify that to the AI coding assistant. For our example, we’re going to use Flask instead. Input the following clarifying prompt, asking the AI to create the server using Flask instead.

Can you create the server using Flask instead, please?

Copy the resulting code and save it into a file named server.py. Save the file and then run it using the following command:

python server.py

Once the server is running, you should be able to see the raw data at the http://127.0.0.1:5000/quotes endpoint. This reveals another potential issue with AI coding for API design. A large quantity of raw, ugly data isn’t that useful. You’ll need to clarify the API with the AI coding platform.

Can you create endpoints for Authors, Categories, or Tags, please?

Copy the resulting code into your existing server file and save it. This should restart your server. You might run into some problems with the server initially, so you might need to troubleshoot the code with the AI coding assistant a little bit before it’s working properly. Once your API server is working, you should have endpoints for /authors, /categories, and /tags.

Now that you’ve seen how to create a working API with AI coding, let’s finish up with a few best practices to give you some ideas on how to use these tools most effectively.

Best Practices for AI Coding for API Design

Learn A Bit About API Design

Although it’s possible to create an API with a simple sentence, it might not be that useful out of the box. In our example, Cursor initially created the Quotes API using Express.js. Knowing more about API design can help give you an idea about different frameworks or libraries you might use to create your API server.

Having a deeper, more nuanced understanding of how APIs work can allow you to create more sophisticated APIs using natural language. Having a deeper understanding of API design can help you come up with prompts like Could you create an abstraction layer for this API, please? or Could you add an authorization flow to this API?.

Understanding how APIs work can help you create more customized APIs using specific prompts. For instance, understanding authorization flows will help you to know how to ask the AI coding assistant to create an authorization flow using Java Web Tokens (JWTs), for instance. Having even a basic understanding of common HTTP error messages will help you to request the right error request for a specific endpoint. Understanding API security and identity management will help you conceptualize how to ask the AI coding platform how to organize your data, which can help prevent common API security risks like Broken Object Level Authorization.

Interact With Your API

After you’ve created your API, you’ll want to use it a bit to see how useful it really is. Early on in our example, we showed how a single endpoint containing all of the raw data isn’t that useful. Using your API can give you further ideas on how to make your API more useful.

For example, interacting with the barebones /quotes might help you realize that returning nearly 50,000 random quotes isn’t that useful. It can give you ideas on how to further fine-tune your API, like adding pagination, or a counter to provide each quote a unique ID to make the script more helpful in delivering random quotes.

Learn By Example

One of the great things about AI coding assistants is the ability to emulate existing products. It’s as easy to ask an AI coding platform to recreate an API as it is to input a prompt. This allows you to benefit from existing products and established wisdom, even if you’re brand new to programming.

We might ask the AI coding platform to recreate an existing API, like this GitHub repository. Simply asking the AI coding assistant a clarifying question and supplying a simple link can make your API exponentially more useful. In this case, providing an example creates numerous endpoints, adding the ability to search and sort data in all manner of useful ways.

Explore Your Data

Once you’ve created your API, you’re free to explore your data however you like. Seeing the relation between different data points unlocks unforeseen relationships. Once you’ve got some working endpoints, you can ask the AI coding platform to customize the code to create custom visualizations, which can give you even more ideas on how to sort and organize your data.

Optimize Your APIs

AI coding assistants aren’t limited to creating new APIs. You can ask the AI coding platform to analyze your existing APIs, as well. You simply have to open your API in your AI coding environment and ask the AI coding assistant to analyze your code.

After it’s analyzed your code, it can even make changes directly to your API, simplifying functions, removing redundant code, and making sure your API is up-to-date with the latest best practices for API design. It’s also a great way to come up with your own APIs, as you can provide the AI coding assistant with the existing API as an example to model the new API after.

Final Thoughts On AI Coding for API Design

AI coding is permanently rewiring how we think about business and technology. At its best, AI coding platforms promise to unleash the long-sought-after goal of creating something out of nothing. No tool is completely useful without knowing how to use it, however.

While AI coding assistants allow you to create a working API using natural language using a single prompt, it’s still a good idea to learn some of the latest best practices for API design to help ensure your API performs as expected.

It’s a good idea to explore the existing products in your sector, as well, which can then be used to further optimize your API. Once you’ve internalized these habits and processes, you’ll have a robust, workable system for designing, testing, and improving your API until it’s an actual, actionable API that can be market-ready far faster than coding every single line by hand.