REST APIs follow predictable patterns — routes, validation, business logic, response shapes — which makes them perfect for AI generation. A good prompt produces consistent endpoints across your codebase; a bad one produces a different style of endpoint every time.
Most REST endpoints are 90% boilerplate: parse the request, validate the input, call the service, shape the response, handle the error. AI can produce that scaffolding in seconds — but only if you give it a clear contract first. This tutorial walks through the route-first prompt pattern: define the contract, then generate the implementation against it.
A REST endpoint is essentially a small contract between client and server: a method, a path, an input shape, a response shape, and a set of possible status codes. If you describe that contract precisely in the prompt, the AI can fill in the implementation with very little ambiguity.
It is the same skill as writing an OpenAPI spec — but in conversational form. Once you internalise that an endpoint is a contract, your prompts become contract specifications and the AI becomes a very fast junior who writes the implementation underneath.
The shortcut prompt is to describe the endpoint in a sentence and hope the AI fills the rest in. It will — but every endpoint generated this way will have a different style, different error shape, and different validation strictness.
Weak prompt
make an endpoint to create a new order
The AI invents a path, picks a framework, designs a request body, chooses a validation library, and uses whatever error format it feels like. None of those choices will match the rest of your API — and every future endpoint generated like this will drift further.
Strong prompt
Stack: Node.js 20, Express 4, TypeScript strict mode.
Validation: zod 3. Error shape (already used across the codebase):
{ error: { code: string, message: string, details?: unknown } }
Auth: requireUser middleware injects req.user: { id: string; role: 'admin' | 'customer' }.
Build POST /api/orders.
Request body (JSON):
{
items: Array<{ productId: string; quantity: number }>, // 1..50 items
shippingAddress: {
line1: string,
city: string,
country: string // ISO 3166-1 alpha-2
},
couponCode?: string
}
Validation rules:
- items non-empty, quantity 1..99 per item
- country is exactly 2 uppercase letters
- couponCode, if present, 4..16 chars, alphanumeric uppercase
Behaviour:
- Authenticated users only (role 'admin' or 'customer')
- Call `await orderService.create(req.user.id, validatedBody)` for business logic
- Service returns an Order or throws OrderServiceError with .code
Responses:
- 201 Created → { order: Order }
- 400 Bad Request → validation error (code: 'INVALID_BODY')
- 401 Unauthorized → no user (handled by middleware, do not duplicate)
- 409 Conflict → service throws code 'OUT_OF_STOCK' (forward message)
- 500 Internal Server Error → unexpected (code: 'INTERNAL_ERROR')
Output: only the route handler file, including the zod schema and a single
exported `router` from `express.Router()`. No tests, no docs.
Every dimension is specified: stack, validation library, error shape, auth, request, validation rules, behaviour, responses. The output will read like every other endpoint in the codebase, because the contract was the same.
Tip: Once one endpoint is set up well, ask the AI to "use this file as a template" when generating the next endpoint. Pasting a reference handler from your own codebase locks in the team's style.
Pick an endpoint you already have in your codebase. Write the prompt that would have generated it, contract-first. Then actually generate it. How close did the AI come to your real implementation? What did each version do better?
Design a new endpoint your project genuinely needs (a search endpoint, an export endpoint, a status check). Write the contract first, then prompt for the implementation. Note the parts you forgot in your contract — they are the parts the AI will quietly make up.
Take your best generated endpoint and ask the AI:
Generate the OpenAPI 3 spec fragment for this endpoint, matching the implementation exactly.
Compare the spec to the code — any mismatches reveal undocumented behaviour.
Sign in to join the discussion and post comments.
Sign inPrompt Engineering for Education & Learning
Use AI as your personal tutor. Learn how to study faster, create lesson plans, generate practice questions, master languages, and prepare for competitive exams with smart prompts.
Prompt Engineering for Image Generation
Turn words into stunning visuals. Master AI image generation tools like Midjourney, DALL·E 3, and Stable Diffusion with 18 focused tutorials — from first prompt to full brand identity.
Prompt Engineering for Data Science & Analytics
Supercharge your data workflows with AI. 15 practical tutorials on using prompt engineering for data cleaning, EDA, machine learning, SQL, visualisation, and more.
Foundations of Prompt Engineering
The must-know basics of prompt engineering. Learn what prompts are, how AI models read them, and how to write clear instructions that get great results.
Prompt Engineering for Content & Copywriting
Write blogs, ads, emails, and social media content ten times faster with AI. 13 practical tutorials on prompt engineering for content creators and copywriters.
Prompt Engineering Projects & Real-World Applications
Twelve hands-on projects that turn prompt engineering theory into a portfolio. Build chatbots, content generators, RAG systems, and more.