<p>I'm going to build an API for a web app and I'm interested in what people can suggest as good practices.</p>

<p>I'm already planning to make it versioned (version 1 can only control certain aspects of the system, version 2 could control more, but this may need a change in the way authentication is performed that would be incompatible with version 1), and the authentication will be distinct from the standard username/password people use to log in (if someone does use a malicious tool it won't open them up to full impersonation, just whatever the api allows).</p>

<p>Does anyone have further ideas, or examples of sites with particularly good APIs you have used?</p>

[{'answer_id': 33737, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 3, 'selected': False, 'text': '<p>Use <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" rel="noreferrer">REST</a>.</p>\n\n<p>RESTful web services architecture is easy to implement and uses the strengths and semantics of HTTP for what they were intended. It\'s resource-oriented, just like the web itself.</p>\n\n<p><a href="http://aws.amazon.com/" rel="noreferrer">Amazon Web Services</a>, Google and many others offer REST APIs to interact with their products.</p>\n'}, {'answer_id': 33741, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I would take a look at proven APIs:</p>\n\n<ol>\n<li><a href="http://code.google.com/apis/youtube/overview.html" rel="nofollow noreferrer">YouTube API</a></li>\n<li><a href="http://apiwiki.twitter.com/" rel="nofollow noreferrer">Twitter API</a></li>\n</ol>\n\n<p>There\'s a lot of argument about whether these APIs are "good" but I think their success is demonstrated, and they\'re all easy to use.</p>\n'}, {'answer_id': 34056, 'author': 'Peter Hilton', 'author_id': 2670, 'author_profile': 'https://Stackoverflow.com/users/2670', 'pm_score': 5, 'selected': True, 'text': '<p>Read the <a href="http://www.crummy.com/writing/RESTful-Web-Services/" rel="noreferrer">RESTful Web Services</a> book, which give you a good overview of how to use REST in practice, and get to up to speed quickly enough to get started now, with some confidence. This is more useful than just looking at an existing API, because it also discusses design choices and trade-offs.</p>\n'}, {'answer_id': 106279, 'author': 'Rich Bradshaw', 'author_id': 16511, 'author_profile': 'https://Stackoverflow.com/users/16511', 'pm_score': 2, 'selected': False, 'text': "<p>Use REST.</p>\n\n<p>Read up on standards for APIs, or copy the ideas from one of the popular ones.</p>\n\n<p>Be careful when authenticating users.</p>\n\n<p>Start very very simple.</p>\n\n<p>Build a site that uses your API (even if it's not useful) to check things work. Perhaps you could build a mobile version of the site or something that forces you to use the API in a lot of depth.</p>\n"}, {'answer_id': 2074949, 'author': 'Peter', 'author_id': 227491, 'author_profile': 'https://Stackoverflow.com/users/227491', 'pm_score': 3, 'selected': False, 'text': '<p>1) Bake the version number directly into the URL rather than passing it as a parameter, since that gives you complete freedom to change the organization of your API namespace with each version bump.</p>\n\n<p>2) Keep your URL rewriting rules (if any) as simple/lean as possible (but no simpler), while making your URLs as beautiful as possible (but no more).</p>\n\n<p>3) Always look for the best HTTP status code you can find for each response (and don\'t forget about 202 and 207, for example).</p>\n\n<p>4) Implement fascist parameter validation logic, and informative error messages.</p>\n\n<p>5) Use HTTP request headers where appropriate instead of parameters (like Accept, for example, to allow clients to specify the desired data format of the response).</p>\n\n<p>6) Organize your "nouns" in such a way that the URLs used by different client audiences are separated near the "root" of your URL tree (this makes it easier to enforce different authentication mechanisms for those different audiences if needed, or even map different portions of your URL tree to different servers).</p>\n\n<p>7) If you\'re serving regular web pages off the same domain as your APIs and use the same authentication credentials, require an X-Requested-With header in your API requests so as to avoid XSRF vulnerabiities.</p>\n'}]
['https://Stackoverflow.com/questions/33728', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1612/']