Prompt Engineering for Better APIs
Learn how to write better prompts that generate more accurate, reliable, and well-documented API endpoints.
Sarah Kim
Dec 18, 2024 · 6 min read
Why Prompts Matter
The quality of your ServeP2E API depends heavily on how you describe it. A vague prompt produces a vague API. A specific, well-structured prompt produces a precise, reliable endpoint.
In this guide, we'll cover techniques for writing prompts that generate better APIs.
The Anatomy of a Good Prompt
A great API prompt has four components:
- Action: What should the API do?
- Input: What data does it receive?
- Output: What should it return?
- Constraints: Any rules or edge cases?
Example: Weak Prompt
Make a currency converterThis is too vague. What currencies? What format? How should errors be handled?
Example: Strong Prompt
Create an API that converts an amount from one currency to another.
Input:
- amount (number, required): The amount to convert
- from (string, required): Source currency code (e.g., "USD")
- to (string, required): Target currency code (e.g., "EUR")
Output:
- originalAmount: The input amount
- convertedAmount: The result rounded to 2 decimal places
- exchangeRate: The rate used for conversion
- timestamp: When the rate was fetched
If an invalid currency code is provided, return an error with
a list of supported currencies.Technique 1: Be Explicit About Data Types
Don't assume ServeP2E knows what type of data you want. Specify it:
- ❌ "takes a date"
- ✅ "takes a date in ISO 8601 format (YYYY-MM-DD)"
- ❌ "returns the price"
- ✅ "returns the price as a number with 2 decimal places"
Technique 2: Define Edge Cases
What should happen when things go wrong?
If the URL is not reachable, return:
{
"status": "error",
"error": "URL_UNREACHABLE",
"message": "Could not connect to the specified URL"
}Technique 3: Provide Examples
Examples help ServeP2E understand exactly what you want:
Example input:
{ "text": "I love this product!", "language": "en" }
Example output:
{ "sentiment": "positive", "confidence": 0.95 }Technique 4: Specify Behavior, Not Implementation
Focus on what the API should do, not how:
- ❌ "Use regex to validate the email"
- ✅ "Validate that the email has a valid format with a username, @ symbol, and domain"
Common Mistakes
1. Being Too Vague
❌ "Make an API for weather"
✅ "Create an API that takes a city name and returns current temperature, humidity, and conditions"2. Forgetting Error Cases
❌ "Convert the image to PNG"
✅ "Convert the image to PNG. If the input is not a valid image, return an error with code INVALID_IMAGE"3. Ambiguous Outputs
❌ "Return whether it's valid"
✅ "Return { isValid: boolean, errors: string[] }"Practice Prompt
Try this exercise: Write a prompt for an API that shortens URLs.
Consider:
- What input does it need?
- What should the output look like?
- What if the URL is invalid?
- What if the URL is already shortened?
- Should it track clicks?
The more specific you are, the better your API will be.
Conclusion
Good prompts lead to good APIs. Take the extra 30 seconds to be specific about inputs, outputs, and edge cases. Your future self (and your API consumers) will thank you.
Need inspiration? Check out our examples page to see well-crafted prompts in action.