Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice for a website to use its own API?

Tags:

api-design

Is it good practice to develop the API while developing the site so the site itself actually uses the API? Or is there a performance hit if choosing to do this?

For example, does anyone know if mature sites such as Facebook or Digg use their own API to CRUD (Create, Read, Update, Delete) or do they have their own backend? Thanks

like image 949
axsuul Avatar asked Aug 28 '10 11:08

axsuul


People also ask

Does my website need an API?

It depends on whether it is necessary to use API. There is not any compulsion that you have to use API which does not make any sense in your application. If your application works completely fine without API then no need to think for that. Keep it simple unless it is needed.

Do most websites have an API?

There are more than 16,000 APIs out there, and they can be helpful in gathering useful data from sites to use for your own applications. But not every site has them. Worse, even the ones that do don't always keep them supported enough to be truly useful. Some APIs are certainly better developed than others.

Does every website have an API?

Not every website has an API, and those that have do not always provide the information you need. Select the data you need and create your own API in minutes.

What makes a good web API?

A good API thinks through its developer experience, providing complete, accurate, and easy-to-digest documentation. It also helps its developers by thinking through common use cases, the sort of things the real user of the API will want.


2 Answers

I doubt Facebook and such use their own API. There are a couple of reasons not to use your own API for the site itself:

  1. You can make data access more performant by using the database directly instead of doing extra requests and (de)serialization.
  2. Probably easier to implement efficient caching with memcached etc
  3. Importantly, you won't have to conform to your public API when extending your site (you don't want to change your public API very often, that'll just annoy everyone)
like image 69
Matti Virkkunen Avatar answered Jan 04 '23 08:01

Matti Virkkunen


I do think it's a good idea to have a low-level interface to the application that you can use without a browser per se, and that the site should use that interface to do its stuff.

That interface doesn't have to be the API itself, it could be a layer that's lower in level than the API, and that is used by both the API and the production website.

It's generally a bad idea if the API just duplicates the website.

i.e., the following is bad

# hypothetical example of bad duplication

def website_update_blog_post(request):
    user = request.username()
    ensure_logged_in(user)
    post = Posts.objects.upsert(request.post_title, request.post_body)
    trigger_notifications(post)

.....

def api_update_blog_post(user, password, title, body):
    verify_login(user, password)
    post = Posts.objects.upsert(title, body)
    trigger_notifications(post)
like image 41
hasen Avatar answered Jan 04 '23 10:01

hasen