How do you keep API contracts stable between a React frontend and evolving services?
Topic: microservices, React Side. Level: Intermediate.
Answer
Version the API (`/v1`) and evolve backward-compatibly (add fields, don't remove/rename). Generate a typed client from the OpenAPI/GraphQL schema so breaking changes surface at compile time, and use **consumer-driven contract tests** (Pact) so the frontend's expectations are verified against providers in CI. The BFF also absorbs shape changes so screens don't break when an internal service changes.
```ts
// Types generated from the backend OpenAPI schema — breaking changes fail the build
import type { paths } from './generated/api'
type Home = paths['/mobile/home']['get']['responses']['200']['content']['application/json']
async function getHome(): Promise<Home> {
const r = await api.get('/mobile/home'); return r.data // shape guaranteed by types
}
```