|
|
|
|
|
by shearn89
5249 days ago
|
|
As @artanis0 (kind of) mentioned, it'd be great to see a HN post sometime soon called "How to write a good 'REST' API". Or even just some links to good tutorials that could get me (and others) started?
I've recently built a DB driven site that could possibly be extended with an API, and it's a good chance to learn something with a purpose behind it! |
|
- Use HTTP verbs: GET to retrieve one or more objects, POST to create a new object, PUT to update an existing object, DELETE to remove an object.
- Address objects by collection and by individual object: /users/#{user_id} is a specific user you can PUT, GET or DELETE. /users is where you POST to in order to create a new user.
- Use HTTP codes to return the result back to the client (ie HTTP 200 when you get an object, 201 when you successfully POST an object, 404 when you try to GET/UPDATE/DELETE an object that doesn't exist).
- I find it good form to return objects in JSON with the type of object at the top of the data structure, ie {:users => [ #array of users here ]} or {:user => { #single user }}
- Use OAuth or some sort of token system for authenticating the calls, don't use HTTP Auth.
You can get pretty anal about things but if you follow the above you'll have a cleaner API than 90% of the API's out there.