Skip to Content

What is the example of URI in REST API?

REST (Representational State Transfer) is an architectural style for building distributed systems based on hypermedia. REST APIs aim to expose data in a predictable way through URIs and HTTP methods. The URI is an essential component of any REST API.

What is a URI?

URI stands for Uniform Resource Identifier. It is a string of characters that identifies a resource on a network. The most common example of a URI is a URL (Uniform Resource Locator) that points to a web page or file on the internet.

A URI has two main purposes:

  • It identifies a resource such as a document, image, or service.
  • It provides a way to locate and access the resource using the network.

Here are some key characteristics of URIs:

  • URIs consist of a scheme, authority, path, query, and fragment.
  • The scheme indicates the protocol used to access the resource, such as HTTP, HTTPS, FTP.
  • The authority specifies the hostname or IP address where the resource is located.
  • The path shows the location of the resource in the host’s file system.
  • The query contains extra parameters that provide input to the resource.
  • The fragment points to a subsection of the resource, like an anchor in an HTML document.

In summary, a URI uniquely identifies a resource and provides the network location where it can be found. This allows clients to locate and interact with resources hosted on servers.

URI Structure

Here is an example of a basic URI structure:

scheme://authority/path?query#fragment

Let’s break down the different parts:

  • Scheme – Defines the protocol used to access the resource, such as HTTP, HTTPS, FTP.
  • Authority – Specifies the domain name or IP address of the server hosting the resource.
  • Path – Shows the path to the resource on the server.
  • Query – Optional parameters that provide input to the resource.
  • Fragment – An optional anchor point within the resource.

The scheme and authority are required, while the path, query, and fragment are optional. The path usually identifies a specific resource, while the query and fragment identify parts within that resource.

Example URI

Here is an example URI:

https://www.example.com/api/v1/users?page=2#profile

This URI can be broken down as:

  • Scheme: https (Hypertext Transfer Protocol Secure)
  • Authority: www.example.com (Domain name)
  • Path: /api/v1/users (Identifies the “users” resource under the API version “v1”)
  • Query: ?page=2 (Specifies to show page 2 of users)
  • Fragment: #profile (Points to the “profile” section within the resource)

So in this example, the URI identifies the users resource provided by the API at example.com. It requests page 2 of the users and accesses the profile section specifically.

URIs in REST APIs

URIs play a central role in REST APIs. They are used to identify the resources the API provides. Some key ways URIs are used in REST APIs:

  • The base URI defines the entry point of the API, like https://api.example.com.
  • Each resource in the API has its own unique URI.
  • URIs use nouns to represent resources, not verbs.
  • Hierarchical URIs allow resources to be nested under parents.
  • Query parameters can filter, sort, and paginate resources.

By providing a URI for every resource, REST APIs enable clients to easily navigate and manipulate those resources using standard HTTP methods like GET, POST, PUT, and DELETE.

Example of URI in REST API

Here is an example of how URIs could be designed in a simple REST API for an e-commerce site:

Base URI

https://api.e-commerce.com

This provides the root URL for all of the API’s resources.

Products Resource

/products

Returns a list of all products.

Individual Product

/products/{productId}

Returns a specific product based on its ID.

Product Reviews

/products/{productId}/reviews

Gets all reviews for the given product.

Product Categories

/categories

Returns a list of all product categories.

Items in Category

/categories/{categoryId}/products

Gets products for the given category.

This showcases some common practices for designing URIs in a REST API:

  • Use plural nouns for collection resources like products and categories.
  • Use singular nouns for individual resources like product.
  • Nest related resources under parents like reviews under product.
  • Use IDs to identify individual resources.
  • Keep URIs short, simple, and easy to understand.

These URIs enable clients to navigate the API resources by following the logical structure exposed through the paths. Additional query parameters could also be appended for filtering, sorting, and pagination.

Best Practices for URIs

Here are some recommended best practices for designing URIs in REST APIs:

Use nouns, not verbs

URIs should represent resources using nouns. They should not try to indicate operations using verbs:

GET /products (Good)

GET /getProducts (Avoid)

Keep URIs concise and consistent

URIs should be kept short and consistent across similar resources:

/products/electronics/televisions (OK)

/products/tvs (Better)

Use lowercase letters

URIs should only use lowercase letters to ensure consistency:

/products/PopularProducts (Avoid)

/products/popularproducts (Better)

Separate words with hyphens

To improve readability, separate words in the path with hyphens instead of underscores, camelCase, or TitleCase:

/popular-products (Good)

/popular_products (Avoid)

Use hierarchical design

Structure URIs in a hierarchical manner to reflect relationships between resources:

/products/electronics/laptops (Good)

This shows laptops as child resources under electronics.

Keep CRUD in mind

Design URIs to support the standard create, read, update, and delete (CRUD) operations.

Advantages of REST URIs

Using proper URIs as outlined above provides the following advantages for REST APIs:

  • Understandable – The URIs clearly reveal the underlying resources.
  • Consistent – Similar URI structures are used for all resources.
  • Extensible – New URIs can easily be appended without affecting existing ones.
  • Scalable – Large numbers of resources can be supported under hierarchical URIs.
  • Cacheable – Caching is easier since resource URIs don’t change.
  • Navigable – Related resources are linked for intuitive navigation.

Overall, well-designed URIs are essential for building REST APIs that are easy to understand and use for clients.

Conclusion

URIs are a fundamental building block of REST APIs. They uniquely identify resources and enable locating them over the network.

URIs should be designed with REST principles in mind – using nouns, consistent naming, hierarchies, and proper CRUD support. Following best practices will result in a clean API with logical URIs.

Well-structured URIs make APIs more scalable, extensible, cacheable, and navigable. Clients can easily interact with resources by combining the URI with HTTP methods like GET and POST.

So in summary, properly designing URIs is an essential aspect of crafting REST APIs that expose resources in a standardized way for easy consumption.