Back to articles
API Versioning: URL vs Header vs Query Parameter

API Versioning: URL vs Header vs Query Parameter

via Dev.to WebdevYoung Gao

API Versioning: URL vs Header vs Query Parameter You shipped v1. Now v2 needs breaking changes. How do you support both without breaking existing clients? Three Approaches Method Example Pros Cons URL path /api/v1/users Simple, cacheable URL proliferation Header Accept: application/vnd.api.v2+json Clean URLs Harder to test Query /api/users?version=2 Easy to switch Easy to forget URL Path (Recommended) import { Router } from " express " ; const v1 = Router (); v1 . get ( " /users " , ( req , res ) => { res . json ( users . map ( u => ({ id : u . id , name : u . name , email : u . email }))); }); const v2 = Router (); v2 . get ( " /users " , ( req , res ) => { res . json ({ data : users , meta : { page , perPage , total }, }); }); app . use ( " /api/v1 " , v1 ); app . use ( " /api/v2 " , v2 ); Sharing Logic Between Versions Do not duplicate business logic. Version the response shape, not the service layer: async function getUsers ( page : number , limit : number ) { return db . query ( "

Continue reading on Dev.to Webdev

Opens in a new tab

Read Full Article
3 views

Related Articles