Custom Apex REST API versioning

Are there any people who already implemented a custom apex REST api that requires versioning in order not to break existing applications who consume the web service? Do you know any best practices around this?

Use case: a third party application at our customers uses a custom API to access Salesforce. At some point there is a request to add/remove parameters or change the response or the logic of the method. This may impact the third party implementations if they are not updated at the same time.

Thx in advance for your ideas.

Answer

For each version of your Custom Rest API service, you should create a separate Class and a separate endpoint. This implies that you should make the API version a component of the URI. For example /services/apexrest/V1/cases and /services/apexrest/V2/cases. These should live in your code base as two separate Classes.

@RestResource(urlMapping='/V1/cases/*')
global with sharing class REST_cases_v1 {
    @HttpGet
    global static Case[] doGet(){
    //Do some Awesome GET service...
    .
    .
    .
    }
    //Do some other awesome methods....
}

AND

@RestResource(urlMapping='/V2/cases/*')
global with sharing class REST_cases_v2 {
    @HttpGet
    global static Case[] doGet(){
    //Do some even Awesomoar VERSION 2! GET service...
    .
    .
    .
    }
    //Do some other awesome methods....
}

Once Version “next” is released, indicate that version “previous” is deprecated and never touch it again! Over time you can work with your consumers and eventually remove the deprecated versions. I’ve been versioning APIs like this for a few years and it’s worked very well for me.

Attribution
Source : Link , Question Author : Nielsm , Answer Author : John Thompson

Leave a Comment