liveBook · Manning

Unique identifier

As explained in The Design of Web APIs, Second Edition

An identifier is a value that uniquely distinguishes an entity or record within a system.

A unique identifier distinguishes a resource within the scope in which it must be found. In an API, an ID, reference, or code can serve this role. A collection path such as /products names a list; adding an identifier, as in /products/123456, identifies one product. The collection name gives the path meaning, while the identifier distinguishes its element. section 4.2.3 section 4.2.5

Identify a product, not a search preference

GET /products/123456 reads a product; PUT or PATCH at that path updates it, and DELETE removes it. If the product does not exist, these operations need a 404 Not Found response. By contrast, a category reference used to narrow GET /products results is a query-parameter filter: products can still be searched without it. The same category reference could be a path parameter when identifying a category itself. section 4.4.3 section 4.4.6 section 4.9

Choose each input’s location by the job it does:

  • Put identifiers needed to identify the operation’s resource in path parameters. Keep create or update representations in the body, and put optional result modifiers in query parameters unless a standard HTTP header fits. section 4.4.7
  • Keep collection and element paths consistent: /accounts and /accounts/{accountId} let consumers construct an element path from its collection path. section 9.3.2
  • Question a required filter. If GET /transactions requires accountId to mean one account’s transactions, GET /accounts/{accountId}/transactions expresses that resource directly. section 9.6.4

How unique is unique?

A globally unique transaction ID makes /transactions/{transactionId} sufficient; repeating customer and account IDs would lengthen the path without helping identify the transaction. An address ID such as home is different: it is not globally unique, so /customers/{customerId}/addresses/{addressId} needs both IDs. Shorten a path only when it still identifies the intended resource accurately. section 9.3.3

Figure 9.3 We question user needs and use globally unique IDs to reduce the number of path parameters in the Banking API resources paths and make them easier to use.
figure

Compare the banking paths to see which parent identifiers can be removed when a child ID is globally unique. section 9.3.3

An identifier also belongs in the resource’s data model alongside its business properties. A generic property name such as id can make that property immediately recognizable across resources, but naming and ID-format decisions still need reasoning and consumer input. As a design check, take one resource path, remove each parent ID in turn, and ask whether the remaining path still identifies exactly the intended resource. Do not impose one ID format on every resource without examining its needs. section 5.2.2 section 16.1.5 section 16.12

How The Design of Web APIs, Second Edition covers it

How deep it goes
Works through path placement, identifier scope, and data-model naming; leaves identifier-format selection as a per-resource design question.
Examples
Uses product URLs, category filters, banking transaction and address paths, and a UUID-policy exercise.
What it assumes
Assumes basic familiarity with HTTP requests while distinguishing identifiers from representations and modifiers through product operations.
What you can do after
Practice/perform identifier selection by placing necessary IDs in paths and removing redundant parent IDs without changing resource meaning.
How it connects
Connects resource identifiers to path parameters, collection resources, query-parameter filters, and resource data models.

Also explained in

Compare how 9 books explain unique identifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.