Skip to main content

Claude/ChatGPT (or Cursor) Prompt to Generate REST API Endpoints

Generate complete REST endpoints: controller, validation, API Resources, routes, feature tests, and OpenAPI docs in your stack.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a senior backend engineer. Generate a complete, production-ready endpoint set and return working code in my framework, not scaffolding stubs.

Context:
- Resource name: orders
- Framework: Laravel
- CRUD actions needed: index, show, store, update, destroy
- Fields to filter by: status, category, date range

Deliver:
1) A controller implementing each action.
2) Form Request validation classes per write action.
3) API Resource transformers for consistent output shapes.
4) Route definitions wired to the controller.
5) Feature tests for every endpoint, including auth and validation failures.
6) OpenAPI/Swagger documentation comments.

Apply JWT authentication, include pagination, filtering by the listed fields, and sorting, and return consistent JSON error responses. Output as copy-ready files grouped by type.

What this prompt does

This prompt makes Claude Code build a complete API slice for one resource instead of a lone controller you then have to wire up by hand. By naming six artifacts explicitly — controller, Form Request validation, API Resource transformers, routes, feature tests, and OpenAPI/Swagger comments — it forces the model to deliver the whole vertical: the request comes in validated, the response goes out shaped, the route is registered, and a test proves each endpoint works.

It works because it's prescriptive, not vague. Generic "write me an API" prompts produce inconsistent shapes and skip the boring-but-critical parts (validation, error envelopes, tests). Here you pin the [framework], [auth_method], the exact [actions], and the [filter_fields], so the output matches your project's conventions rather than the model's defaults. The explicit asks for pagination, sorting, and consistent JSON error responses close the gaps that usually become bug tickets later.

When to use it

  • Scaffolding a brand-new resource (Product, Booking, Order) and you want validation, tests, and docs in one pass.
  • Standing up a public or mobile-facing API where pagination and filtering are required, not optional.
  • Replacing an inconsistent hand-rolled endpoint with a conventional controller + Form Request + Resource trio.
  • Onboarding a teammate who needs a reference implementation that follows your house style.
  • Building the read API a Flutter or SPA client will consume, where stable response shapes matter.

Example output

For Create a complete RESTful API for Product in my Laravel project … index, store, show, update, destroy … Sanctum auth … filter by category_id, status, Claude Code returns:

app/Http/Controllers/Api/ProductController.php   (5 actions, eager-loaded)
app/Http/Requests/StoreProductRequest.php        (rules + messages)
app/Http/Requests/UpdateProductRequest.php
app/Http/Resources/ProductResource.php
tests/Feature/ProductApiTest.php                 (one test per endpoint)
routes/api.php  → Route::apiResource('products', …)
                  ->middleware('auth:sanctum')

// index: ?category_id=3&status=active&sort=-created_at&page=2
{ "data": [ … ], "meta": { "current_page": 2, "total": 57 } }

Pro tips

  • Set [filter_fields] to an explicit allow-list and ask the controller to validate against it — never let raw query params reach the query builder, or you've shipped a filtering injection.
  • Keep the Form Requests even though the controller validates: your Filament resources and admin forms can reuse the same rules, so you get one source of truth.
  • Tell it which paginator you want (paginate vs cursorPaginate); cursor pagination is the right default for large, mobile-fed lists.
  • Make the tests assert assertJsonStructure and the error envelope, not just assertOk — that's what catches a Resource field rename before production does.
  • Pair this with a follow-up "now add rate limiting and an idempotency key to store" once the slice is green.

Frequently Asked Questions

Does this prompt actually write the tests, or just stub them?
It generates real PHPUnit feature tests — one per endpoint — that hit the route, assert the status code, and check the JSON shape. Treat them as a strong starting point: run them, then add your edge cases (auth failures, validation errors, empty filters) since the model can't know every business rule.
What do I put in [auth_method] and does it change the code much?
Use whatever your project uses — `Sanctum`, `JWT`, `Passport`, or `API key middleware`. It mostly changes the route middleware and the test setup (how the request is authenticated). The controller, Form Requests, and Resources stay the same, so swapping auth later is cheap.
Will the OpenAPI comments produce a usable Swagger doc?
It generates annotation-style docblocks (e.g. l5-swagger / zircote format) that are accurate to the endpoints it just built — but they only render a UI once you've installed and configured the generator package. Tell it which annotation flavor you use so the comments compile cleanly.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Claude Code Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support