D
NetworkingEasy20 XP2 min read

How do you handle API versioning in a Flutter app?

TL;DR: Include the API version in the base URL (https://api.example.com/v2) or as an Accept header (Accept: application/vnd.api+json;version=2). Configure via DI/environment so you can switch without code changes.

Full Answer

API versioning strategies: URL path (/v1/, /v2/), query parameter (?version=2), and Accept header versioning. Path versioning is most common and easiest to debug.

  • URL versioning: baseUrl = 'https://api.example.com/v2' — clear, cacheable, easy to test
  • Header versioning: 'Accept: application/vnd.example+json;version=2' — cleaner URLs, harder to test
  • Feature flags: Keep old models, add new fields optionally — for gradual migration
  • Environment config: Inject baseUrl via flavor or .env — different versions per environment
🎯

When migrating from v1 to v2, maintain both API clients simultaneously. Migrate feature by feature rather than a big bang switch to reduce risk.

Code Examples

dartVersioned API client setup
Output
// Production: /v2/questions
// Staging: uses same version against staging server

Common Mistakes

  • Hardcoding /v1/ in every endpoint string — when you move to v2, you update hundreds of strings
  • Not testing backward compatibility — if users don't update, they still call v1 endpoints

Interview Tip

💡

Mention that mobile apps can't force updates, so v1 endpoints must stay alive until usage drops to near zero. Show you understand the mobile lifecycle constraint.

#api-versioning#v1#v2#backward-compatibility