Volume 10 · Chapter 6 — Advanced .NET Engineering, Modern Development & AI-Assisted Software Engineering

Modern API Design

Goutham Mayaluri 8 min read .NETC#

6.1 Introduction

APIs are the contracts through which systems, teams, and external partners integrate, and a well-designed API is a long-lived asset while a poorly designed one is a permanent source of friction. Modern .NET offers several styles - REST, gRPC, GraphQL - each fitting different needs. Good API design is less about the style and more about clear contracts, thoughtful versioning, security, and resilience. This chapter covers choosing a style and designing APIs that last.

6.2 REST and Resource-Oriented Design

REST models the domain as resources addressed by URLs and manipulated with HTTP verbs and status codes. Done well - noun-based resources, correct verbs, meaningful status codes, statelessness - it is intuitive, cacheable, and universally supported, which is why it remains the default for public and browser-facing APIs.


app.MapGet("/accounts/{id:guid}", GetAccount);          // 200 or 404
app.MapPost("/accounts/{id:guid}/transfers", Transfer)  // 201 Created / 400 / 409
   .RequireAuthorization("payments.write");

6.3 API Versioning

APIs must evolve without breaking existing clients, which makes versioning a first-class design concern. The strategies - URL path (/v2/...), header, or query parameter - trade discoverability against URL cleanliness. Whichever you choose, the discipline is the same: make additive changes backward-compatible and reserve new versions for breaking changes, running old and new in parallel during migration.

6.4 Minimal APIs, gRPC, and GraphQL

Minimal APIs reduce ceremony for lightweight HTTP services and cloud-native microservices. gRPC (HTTP/2 + Protobuf) gives high-performance, strongly typed, streaming service-to-service communication - ideal internally. GraphQL lets clients request exactly the fields they need in one round trip, solving over- and under-fetching for complex, data-hungry frontends, at the cost of server-side complexity and harder caching. The choice follows the consumer and call pattern.

Style Best for Trade-off
REST/JSON Public, browser, broad compatibility Over/under-fetching; multiple round trips
gRPC Internal service-to-service, high throughput Weaker browser support, binary (less debuggable)
GraphQL Complex frontends, aggregated data Server complexity, caching, query cost control

6.5 Security, Rate Limiting, and Resilience

Every API enforces authentication and authorization (Volume 9, Chapter 9), validates input, and uses TLS. Rate limiting (fixed/sliding window, token bucket) protects against abuse and overload and enforces fair use. And because APIs call other services, they need resilience - timeouts, retries, circuit breakers, fallbacks (Volume 7, Chapter 8) - so a dependency’s failure does not become the API’s failure.

6.6 API Governance and Documentation

At enterprise scale, consistency across APIs is itself a feature: shared standards for naming, error formats (for example RFC 7807 problem details), pagination, versioning, and auth mean any consumer can learn one API and understand the rest. Machine-readable contracts (OpenAPI) drive documentation, client generation, and contract testing, turning the API into a governed, discoverable product rather than a bespoke artifact.

Senior Interview Question

How do you approach API versioning to avoid breaking consumers?

I treat the published API as a contract that clients depend on and deploy on their own schedules, so the core rule is: never make a breaking change in place. Additive, backward-compatible changes - new optional fields, new endpoints - do not require a new version and existing clients keep working. When a genuinely breaking change is unavoidable - removing or renaming a field, changing semantics - I introduce a new version (via URL path, header, or query parameter, chosen for the ecosystem), run the old and new versions in parallel during a clearly communicated migration window, and retire the old only after consumers have moved, ideally verified with usage telemetry and consumer-driven contract tests. I design responses to be tolerant (clients ignore unknown fields) so additive changes are absorbed gracefully. The combination - additive-by-default, explicit versioning for breaking changes, parallel running, and contract testing - lets the API evolve while consumers upgrade on their own timeline, which is the whole point of versioning.

Senior Interview Question

When would you choose gRPC or GraphQL over REST?

I choose by the consumer and the call pattern. REST/JSON is my default for public, browser-facing, and broadly consumed APIs because it is universal, human-readable, cacheable, and simple. I choose gRPC for internal service-to-service communication that is high-frequency or latency-sensitive: its HTTP/2 transport and Protobuf binary encoding give lower latency, smaller payloads, strong typed contracts, and first-class streaming - excellent in a microservice mesh, though its browser support and debuggability are weaker. I choose GraphQL when the consumers are complex frontends that need to fetch varied, nested data efficiently: it lets the client request exactly the fields it needs in a single round trip, eliminating the over-fetching and multiple-round-trip problems REST can cause for rich UIs, at the cost of server-side complexity, harder HTTP caching, and the need to control expensive queries. So: REST for broad/public, gRPC for internal high-performance, GraphQL for flexible client-driven data needs - and large systems often use all three at different boundaries.

Senior Interview Question

What makes a REST API well-designed?

Clear, consistent contracts that follow HTTP’s semantics. Resources are modeled as nouns with sensible URL hierarchies, the correct verbs express intent (GET is safe and idempotent, POST creates, PUT/PATCH update, DELETE removes), and status codes are used meaningfully (200/201/204, 400/401/403/404/409, 500) so clients can react programmatically. It is stateless, so any instance can serve any request and it scales horizontally. It handles errors consistently with a structured format like problem details, supports pagination and filtering for collections, and versions changes without breaking clients. It is secured (authN/authZ, TLS, input validation) and documented via a machine-readable contract (OpenAPI) that drives docs, client generation, and contract tests. Above all it is consistent - naming, error shapes, pagination, and auth work the same across endpoints - because consistency is what lets a consumer learn one part and understand the whole. Good REST design is disciplined adherence to these conventions, which makes the API predictable, evolvable, and pleasant to consume.

Principal Engineer Question

How do you establish and enforce API standards across many teams in a large enterprise?

I make consistency a governed, largely automated property rather than a matter of each team’s taste. First I define a concise, opinionated API style guide covering the things that must be consistent for consumers: resource naming, versioning strategy, a standard error format (problem details), pagination and filtering conventions, authentication/authorization patterns, and documentation requirements. Then I make the standard the path of least resistance - shared templates, libraries, and scaffolding that bake in the conventions (auth, error handling, versioning, OpenAPI generation) so teams get compliance for free by building on the paved road. I enforce it with automation: linting OpenAPI specs against the style guide in CI, requiring a published machine-readable contract, and using consumer-driven contract tests (Pact) so producer changes cannot silently break consumers. I add lightweight governance - a design-review step or an API guild for significant new APIs - and a central catalog/portal so APIs are discoverable as products, not hidden endpoints. The aim is that any engineer can consume any internal API by learning the shared conventions once, and that teams retain autonomy over their domain while the cross-cutting shape is standardized - guardrails and tooling delivering consistency without turning governance into a bottleneck.

Principal Engineer Question

An internal API is chatty - clients make many calls to render one screen - hurting performance. How do you address it?

Chattiness usually means the API’s granularity is mismatched to how clients actually use it, so I diagnose the real usage pattern before changing anything. If a particular client (say a mobile app) needs an aggregated view assembled from several services, the clean fix is a Backend-for-Frontend or an aggregation/composition layer that makes the fan-out server-side, close to the services, and returns exactly the shape the client needs in one round trip - turning many high-latency client calls into one. Where clients need flexible, varied slices of nested data, GraphQL is a strong option because it lets each client request precisely the fields it needs in a single request, directly solving over-fetching and multiple round trips. I might also add coarser-grained resource endpoints or expansion parameters to REST so a screen’s data comes back together, and apply caching for stable parts. I’m careful not to over-correct into giant god-endpoints that couple unrelated concerns; the goal is to match API granularity to genuine client needs, keep the aggregation on the server side where network latency is low, and measure the improvement. The underlying principle is that API design should serve its consumers’ interaction patterns, and BFFs, aggregation, or GraphQL are the tools for reconciling fine-grained services with coarse-grained client needs.

6.7 Banking Enterprise Scenario

A bank exposes REST/JSON APIs for public and partner consumption with strict standards - consistent resource naming, problem-details errors, header-based versioning, OAuth2/OIDC auth, and OpenAPI contracts that drive docs and contract tests. Internally, high-throughput service-to-service calls use gRPC for performance and typed contracts. A mobile app that once made many chatty calls now goes through a Backend-for-Frontend that aggregates account, balance, and transaction data server-side into one response. Every API is rate-limited, resilient (timeouts, circuit breakers), and versioned so it can evolve without breaking the bank’s many consumers - APIs treated as governed, long-lived products.

Chapter Revision Notes

  • Good API design is about clear contracts, versioning, security, and consistency - not the style.
  • Version additively; reserve new versions for breaking changes and run them in parallel.
  • REST for public/broad, gRPC for internal high-performance, GraphQL for flexible client data.
  • Secure, rate-limit, and make APIs resilient; a dependency failure must not become an API failure.
  • Enforce enterprise standards with shared templates, OpenAPI, linting, and contract tests.
  • Fix chatty APIs with BFFs/aggregation or GraphQL, matching granularity to client needs.