|
| 1 | +# Deprecating API endpoints |
| 2 | + |
| 3 | +As for deprecation indication using HTTP status code there is no clear specification. |
| 4 | + |
| 5 | +Various answers on StackOverflow and the like tend towards: `410 Gone`. |
| 6 | + |
| 7 | +Some suggest using: `301 Moved Permanently` or `302 Found`. |
| 8 | + |
| 9 | +I do see some challenges with: `401` as it should indicate that the resource pointed to no longer exists. |
| 10 | + |
| 11 | +I believe the HTTP status code: `501 Not implemented` could be used but is not the best solution BUT it could be combined with redirects. Alternatively: `405 Not Allowed` might be a better and with better control with assistance: `Allow` header to solve this: |
| 12 | + |
| 13 | +- You have an API and a resource which can be fetched via: `GET`, it would normally return the resource and the HTTP status code: `200 Ok` |
| 14 | + - if the resource was deleted you should return: `410 Gone` |
| 15 | +- If the API is deprecated one could return `308 Permanent Redirect` for a period of time indicating the replacement via the `Location` header, like a newer version: `/api/v1` to `/api/v2` and this would work for both: `POST` and `GET` where `301 Moved Permanently` only works for `GET` |
| 16 | + - If no replacement is in available return: `405 Not Allowed` |
| 17 | +- In due time and after adoption have been made for the new API endpoint, use: `405 Not Allowed` since the API endpoint is gone and it will not come back |
| 18 | + |
| 19 | +For the Swagger part, do see my TIL on that: |
| 20 | + |
| 21 | +- [Deprecating API endpoints](../swagger/deprecating_api_endpoints.md) |
| 22 | + |
| 23 | +## Resources and References |
| 24 | + |
| 25 | +- [MDN: HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status) |
| 26 | +- [MDN: 301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301) |
| 27 | +- [MDN: 302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/302) |
| 28 | +- [MDN: 308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308) |
| 29 | +- [MDN: 405 Method Not Allowed](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/405) |
| 30 | +- [MDN: 410 Gone](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/410) |
| 31 | +- [MDN: 501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/501) |
| 32 | +- [StackOverflow: "What is the correct HTTP status code for: "This version of this API has been discontinued"?](https://webmasters.stackexchange.com/questions/71152/what-is-the-correct-http-status-code-for-this-version-of-this-api-has-been-dis) |
0 commit comments