What is a URL?
A URL is a Universal Resource Locator, identifying the web address where a specific resource can be found on the world wide web, and how to access the resource. This resource can be an HTML web page, a CSS document, an image, etc.. They are used in REST APIs to address resources to developers using an API. You may have seen URIs, URLs, URNs, and domain names in networking discussions before, but how do we tell them apart?
- The URI (Universal Resource Identifier) is a string identifier that refers to a resource on the internet, like a specific document.
- The URL (Uniform Resource Locator) is the location of the resource on the web, and it tells you how to access the resource (`https://`, for example).
- The URN (Uniform Resource Name) is a unique identifier for a single object, meant to be long-term even when the resources the URN identifies are gone.
URL Syntax
A URL is an identifier with syntax that looks like this: `scheme:[//authority]path[?query][#fragment]`. Let's have a look at the parts of the URL.
- Scheme: This identifies how the resource is accessed. Examples include http, file, ftp, data, and irc.
- Authority: Authority is optional and preceded by two slashes `//` like a comment. It contains the subcomponents `userinfo`, `host`, and `port`.
- Path: A sequence of path segments separated by a slash(`/`). The URI always specifies it; however, the specified path may be empty or of 0 lengths.
- Query: An optional component preceded with a question mark (`?`) that contains non-hierarchical data.
- Fragment: An optional component preceded by a hash (`#`) symbol. It provides direction to a secondary resource.
URL Base Server
In an API endpoint, the URL is relative to the base URL. For example, in the URL `https://exchange-rates.abstractapi.com/v1/historical`, the `/historical` endpoint refers to `https://exchange-rates.abstractapi.com/v1/historical`. Servers have a slightly different URL syntax, but the high-level idea is the same: it is an address where a resource can be found.
URL Syntax Best Practices
URLs are common resource locations in REST APIs, so naming them descriptively is considered a best practice. A developer using your REST API may not know what a resource does or what it contains, so naming it a string of letters and numbers is not helpful to them. Consider naming it descriptively, for example, the above endpoint is called `https://exchange-rates.abstractapi.com/v1/historical`. This endpoint allows us to access historical exchange rate data. We can also convert currency at `https://exchange-rates.abstractapi.com/v1/convert`, or view live currency exchange rates at `https://exchange-rates.abstractapi.com/v1/live`. This follows the URL syntax above, and gives new users a clear path to follow.
Conclusion
URLs display where a resource is available and how it is accessed. If you click one of the links in this article, it will carry you away to exactly where the resource is located. If you call the exchange rate API, you send your request to the URL and receive resources back. URLs are the address of a resource, found in the address bar of your web browser- not the IP address, which is a unique number attached to your hardware connected to a network.