雪花台湾

Apigee Pragmatic API Design 3 – API Version and Error Handling

API Version Number

 

Never release an API without a version and make the version mandatory(強制的).

 

API can use Version number placed somewhere in the middle of the URL.

 

Specify the version with a ‘v’ prefix and don’t use the dot notation like V1.2 because it implies(意味) a granularity(顆粒度) of versioning that doesn’t work well with APIs. API is an interface not an implementation. Stick with(堅持做) V1, V2, and so on.

 

Move Version Number all the way to the left in the URL so that it has the highest scope.

 

How many versions should we maintain? Maintain at least one version back.

 

Should version and format be in URLs or headers?

  If it changes the logic you write to handle the response, put it in the URL so you can see it easily.

  If it doesn’t change the logic for each response, like OAuth information, put it in the header.

 

API Error Handling

 

The error message returned in the payload as verbose as possible as below,

{

  “Status” : ”Standard HTTP status Code.”,

  “DeveloperMessage” : “Verbose, plain language description of the problem for the app developer with hints about how to fix it .”,

  “UserMessage” : ”Pass this message on to the app user if needed.”,

  “ErrorCode” : ”Defined code for developer to mapping with developer message ”,

  “MoreInfo” : “ Refer API Document ”

}

 

Choose HTTP Status Code

 

There are really only 3 outcomes in the interaction between an app and an API,

Success / Client Error / Server Error .

 

Start by using the following 3 codes. If you need more, add them. But you shouldn’t need to go beyond 8.

200 – OK (Success)

400 – Bad Request (Client Error)

500 – Internal Server Error (Server Error)

 

if you’re not comfortable reduciong all your error conditions to these 3, try picking among these additional 5,

201 – Created

304 – Not Modified

404 – Not Found

401 – Unauthorized

403 – Forbidden

 

 

201868日星期五

相关文章