23

I'm designing a REST API for a project where users are always on one of several "plans" - each plan defines some resource limits, such as the max number of users an account may have or the max number of data they may upload. Once one of these limits is reached, users can upgrade their plans (essentially pay up) to get more resources.

I want to return a special status code indicating a situation where the action cannot be performed due to account resource limits, and upgrading the plan will resolve this - for example if a user uses 100% of their storage capacity and try to upload an additional file, they will get this response.

The candidates are, IMHO:

  • 403 Forbidden - however, I would like to distinguish between this case and other cases where the user simply lacks the permission to perform this action.

  • 401 Unauthorized - not a good idea, we're using this for authentication related problems.

  • 402 Payment Required - makes kind of sense but I'm worried about using a non-standard yet reserved status code

  • Something even less standard like 423 Locked as its unlikely we'll use it for anything else in the future

Another option is to go with something very standard such as 403 but indicate the specifics of the error in the response body.

I'm wondering which approach you believe would (a) work best in the long run and (b) would stick more nicely to RESTful principles.

  • 1
    There is HTTP 507 Insufficient Storage. – CodesInChaos Jul 1 '15 at 9:42
  • RFC4331 might be relevant, it's about quota limits for WebDAV. – CodesInChaos Jul 1 '15 at 9:45
  • @CodesInChaos this shouldn't be a 5xx error, and storage was only an example (the real project is not about storage at all in fact, it was just a good analogy). – shevron Jul 1 '15 at 12:32
17

I think 403 is the only reasonable response, though 405 Method Not Allowed or 409 Conflict might be acceptable, I don't think either are as good as 403 which states:

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity

If you return a 403 error, it'll include some information on why the resource was denied - invalid permission is only the most common case, exceeded limits isn't much different - you don't have permission because your limit was exceeded.

21

I believe 403 is wrong, because 403 is for situations where you aren't getting access to the resource, and there is no way whatsoever to get access. For your customers, there is obviously a way to get access: Pay up.

401 is truly wrong, because not only are you using it for authentication, but that is what it is there for.

Since you are writing an API, I assume that someone else will have to write code that uses the API, and that person needs to read your API spec. You might go with 429 "Too many requests". It is usually intended for rate limiting (where a client can make 100 requests per day, for example), but does reasonably apply to your situation. 402 (Payment required) would also be acceptable, I think. Depends on what tools you expect people to use to use your API. 429 has the risk that a clever tool tries to send fewer requests per minute/hour/day and never succeeds.

BTW according https://tools.ietf.org/html/rfc6585 the 429 error should also contain an html message describing the nature of the problem, so there's a good chance that the user is actually told what the problem is, if you supply that information in your response.

  • 1
    402 is an option but I prefer to reserve 429 for actual rate limiting purposes which we are likely to add in the future – shevron Jul 1 '15 at 12:34
  • Google seems to use 403 though I like 429 much better. I've seen some custom implementations of http clients that did some weird stuff on 401 and 403 (for example a website would log the user off if it ever got 401 or 403 from the api). – Cristian Vrabie Oct 11 '16 at 11:21
0

WebDAV uses HTTP 507 Insufficient Storage for this and includes an additional error code for quota exceeded in the request body, to distinguish it from other kinds of storage limitations.

  • 12
    It seems counterintuitive to use a 5xx code for this. – Ben Aaronson Jul 1 '15 at 10:00

protected by gnat Jul 25 '17 at 12:57

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.