agentleFS
kanciak0kanciak0/claude-skills/skills/dotnet-api-template/SKILL.md

dotnet-api-template

Skill0 starsChanged 3 months agoMITView on GitHub

Stand up a .NET Minimal API with a real server-client contract — explicit DTOs, typed results, source-generated JSON, OpenAPI and route versioning — instead of endpoints returning anonymous objects. Use whenever someone is adding or building an HTTP API in C#/.NET, writing endpoints, exposing data over REST, wiring EF Core behind endpoints, or adding a live stream over SSE. Trigger on phrases like "add an API", "create the endpoints", "expose this over HTTP", "minimal API", "wystaw to przez API", "zrób endpointy", "dorób REST" — and also when someone complains that their frontend is guessing field names, that the API has no contract, or that changing the server silently broke the client.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Ask two questions first, then build — they change the file list, and guessing wrong wastes more time than asking:

    line 33
  2. Very specific:

    If the project doesn't exist yet, scaffold it first with the dotnet-project-scaffold skill, then come back here.

    line 40
  3. Distinctive:

    Run the app and check all three, because each fails independently:

    line 54

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

.NET Minimal API with a real contract

The default way to write a Minimal API endpoint is to return an anonymous object:

app.MapGet("/api/devices", (Store store) =>
    Results.Ok(store.All().Select(d => new { imei = d.Imei, speed = d.Speed })));

It works, it's three lines, and it has no contract. Nothing tells you what the response looks like except reading the lambda. Rename speed to speedKmh and every client breaks silently — no compiler error, no failing test, just undefined in a browser somewhere. There's nothing to generate client types from, and no way to add a second version later without breaking the first.

The fix is small and worth doing from the first endpoint, because retrofitting it once clients depend on the shape is where the real cost is.

The five pieces

1. Explicit response types, not anonymous objects. A public sealed record per response, in Contracts/, with required on everything that is always present and a nullable type on everything that isn't. Now the shape is a declaration a reader can find, and the compiler enforces it.

2. The type in the handler signature. Ok<IReadOnlyList<DeviceSummary>> rather than IResult. This is what makes the contract enforceable rather than aspirational — the response type is part of the method signature, so changing it is a visible edit, and OpenAPI can describe it without guessing.

3. Source-generated JSON. A JsonSerializerContext listing every type that crosses the wire, registered ahead of the reflection resolver. Anything not declared fails loudly instead of serializing to {} at runtime. It also removes reflection from the hot path and keeps the app trimmable.

4. OpenAPI. AddOpenApi() and MapOpenApi() publish the contract at /openapi/v1.json in a form a client generator can consume — so the frontend's types come from the server rather than being retyped by hand and drifting.

5. A version in the route from day one. /api/v1/.... Adding /api/v2 beside a live /api/v1 is easy; introducing versioning after clients depend on unversioned routes is not. It costs four characters now.

What to do

Ask two questions first, then build — they change the file list, and guessing wrong wastes more time than asking:

  1. Is there persistence behind these endpoints? (adds Data/ and EF Core wiring)
  2. Does anything need to push to the client — live updates, progress, a feed? (adds the SSE endpoint)

Then read references/templates.md and write the files. It has the complete contents for each one: Contracts/, Endpoints/, Program.cs, the EF Core variant, and the SSE variant. Adapt the names to the domain — the templates use Device and Position as stand-ins, and leaving those in a real project looks exactly like what it is.

If the project doesn't exist yet, scaffold it first with the dotnet-project-scaffold skill, then come back here.

Things that go wrong

Returning entities from endpoints. The most common shortcut and the one that costs most later: it welds the storage schema to the public contract, so a column rename becomes a breaking API change. Map entities to DTOs in one place — a static mapper class beside the contracts — and keep the two vocabularies separate.

A type missing from the JsonSerializerContext. Symptom is an empty object or a runtime exception the first time that endpoint is hit. Every type crossing the wire needs its own [JsonSerializable], including collection types like IReadOnlyList<T>.

Backpressure on a live stream. If clients get an unbounded channel, one slow browser grows the server's memory until it dies. Give each subscriber a bounded channel in DropOldest mode and publish with TryWrite, which never blocks. A viewer that can't keep up should lose frames — it must never be able to slow down the work the server actually exists to do. This is the opposite of what you want on an ingest path, where backpressure is the point and data must survive; being deliberate about which side you're on is the whole decision.

Validation living in the handler. Handlers stay thin — parse, delegate, map. Validation belongs in the request type or a filter, so it's testable without an HTTP request.

Verify before saying it works

Run the app and check all three, because each fails independently:

curl -s localhost:5080/openapi/v1.json | head -40   # contract is published
curl -s localhost:5080/api/v1/<resource>            # shape matches the DTO
curl -N -s localhost:5080/api/v1/live               # stream, if you built one

A compile is not a verification. Source-generated JSON and SSE both fail at runtime, not at build time, so an API that builds cleanly can still return {} to every request.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.