---
name: python-rest-api-formatter
description: "Generates one FastAPI endpoint with Pydantic request and response models from a field list: typed path parameters, validated body, declared status codes, optional auth dependency, all in a single pasteable code block. Use when one FastAPI endpoint needs Pydantic request and response models, typed path parameters, declared status codes, and an optional auth dependency. Do not use for a whole API project, Flask endpoints, or validation middleware in another framework. Related topics: coding, python."
---

# Python REST API Formatter

Generates one FastAPI endpoint with Pydantic request and response models from a field list: typed path parameters, validated body, declared status codes, optional auth dependency, all in a single pasteable code block.

## When to use

Use this skill when one FastAPI endpoint needs Pydantic request and response models, typed path parameters, declared status codes, and an optional auth dependency. Do not use it for a whole API project, Flask endpoints, or validation middleware in another framework.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Build the Request and Response Pydantic models from the field lists with matching type hints, treating GET and DELETE request fields as query parameters.
3. Extract path parameters from the URL and declare them typed in the signature.
4. Declare the route with response_model, the primary success status_code, and a responses dictionary for every other selected code.
5. Add the bearer-token Depends stub raising 401 when auth is required and a 404 raise where a lookup can miss.
6. Append the example request comment and output a single python code block with nothing outside it.

## Inputs

- **Endpoint name** (free text, e.g. "get_user")
- **HTTP method** (choose one: GET, POST, PUT, DELETE, PATCH)
- **URL path** (free text, e.g. "/users/{id}")
- **Request fields (one per line)** (longer free text)
- **Response fields (one per line)** (longer free text)
- **Authentication required** (choose one: Yes, No)
- **Status codes** (choose any: 200 OK, 201 Created, 400 Bad Request, 401 Unauthorized, 404 Not Found, 500 Internal Server Error)

## Output

Exactly one markdown code block tagged python: imports, the Pydantic Request and Response models, the auth dependency when required, the decorated route function, and a closing example-request comment.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Python REST API Formatter

Generate a single FastAPI endpoint with Pydantic models from this specification.

- Endpoint name: {{Endpoint name}}
- HTTP method: {{HTTP method}}
- URL path: {{URL path}}
- Request fields (one per line): {{Request fields (one per line)}}
- Response fields (one per line): {{Response fields (one per line)}}
- Authentication required: {{Authentication required}}
- Status codes: {{Status codes}}

Build two Pydantic models from the field lists, Request and Response, with Python type hints that match the given types and Optional for anything the list marks optional; for GET and DELETE, the request fields become query parameters instead of a body model. Pull path parameters out of the URL path and declare them typed in the function signature. Declare the endpoint with the method decorator, the path, response_model set to the Response model, status_code set to the primary success code from the selection, and a responses dictionary describing every other selected code so the generated docs show them. When authentication is required, add a Depends on a bearer-token dependency stub and raise HTTPException 401 from it. Raise HTTPException 404 where the handler looks up a resource that may be missing. Keep the handler body a clear placeholder with a comment where the data access goes. Finish with a short comment showing an example request for the endpoint. Return only one markdown code block tagged python containing the imports, the models, the dependency when used, the route, and the example comment, with no text outside the block.

## Pitfalls

- Declaring a body model on a GET endpoint, which FastAPI accepts but every HTTP client and proxy mishandles.
- Setting status_code to 200 on a POST that creates a resource when 201 was selected.
- Leaving a path parameter untyped so a non-numeric id reaches the handler instead of failing validation with 422.
