0

Why do API's use different URLs? Is there two different interfaces on the web server? One processing API requests and the other web HTTP requests? For example there might be a site called www.joecoffee.com but then they use the URL www.api.joecoffe.com for their API requests. Why are different URLS being used here?

jssteele89
  • 463
  • 1
  • 4
  • 11
  • Not all APIs do that, and whether to use one technique or the other is [up for debate](https://stackoverflow.com/questions/14554943/is-it-better-to-place-a-rest-api-on-a-subdomain-or-in-a-subfolder), and opinion based, – James Aug 18 '17 at 03:42

1 Answers1

2

We separate ours for a couple of reasons, and they won't always apply.

Separation of concerns.

We write API code in one project, and deploy it in one unit. When we work on the API we only worry about that and we don't worry about page layout. When we do web work, that's completely separate

Different authentication mechanisms.

The way you tell a user to log in is quite different to how you tell an API client it's not authenticated.

Different scalability requirements

It might be that the API does a lot of complex operations, while the web-server serves more or less static content. So you might want to add hundreds of API servers around the world, but only have 10 web servers.

Different Clients

You might have an API for the web client and a separate API for a mobile client. Or perhaps a public one and a private / authenticated one. This might not apply to your example.

Different Technologies

Kind of an extension of Separation of concerns, but it allows you to have Linux server for one and use something like an AWS Lambda for the other.

SSL Wrangling

This one is more of an anti-reason (particularly for the specific example you give). Many sites use SSL for both web and api. Most sites are going to use SSL for the API at least. You tend to have SSL certificates matched to your URL, so there might be a reason there. That said, if you had a *.joecoffee.com certificate you would use api.joecoffee.com not www.api.joecoffee.com (because apparently an extra '.' in your URL costs more, or something like that).

As @james suggested - there's no really right answer and some debate.

GregHNZ
  • 7,946
  • 1
  • 28
  • 30