REST API Design – Best Practices for Identifiers with Slashes

apiapi-designrest

I am designing a REST api for a document management system. To make the API more natural, I thought about using the natural identifier for a file, which is it's path (usually having forward slashes "/" in it), and not something like an artificial id such as a UUID.

Naturally, a user would identify a file by it's path:

/path/to/file.txt

Unfortunately, this clashes with common, good API design.

This would work:

Retrieving a file: GET /api/v1/files/<path/to/file.txt>

However, other methods you would like to have, will become a bit problematic:

Retrieving a file list of a folder: GET /api/v1/folders/<path/to/folder>/files

My thoughts so far:

  • avoid unique identifiers with "/" in it entirely, and use an artificial UUID (such as Alfresco is using NodeId / NodeRefs). This would add another layer of abstraction to it, where a unique id is just not very natural from a business domain perspective
  • don't use the identifier in the url path but in a parameter (against good API design patterns)
  • (requesting the API users to replace the / with another character is of course also not an option)

Of course, files and folders as we know it from a file system, are naturally identified by it's path. In that particular case, one could argue that if someone wants to use the path as the identifier, than the correct way for doing:

Retrieving a file: GET /api/v1/files/<path/to/file.txt>

would actually be:

Retrieving a file: GET /api/v1/folders/<path>/folders/<to>/files/<file.txt>

if we want to be really correct here.

Update: I want to ask the community about best practices and experiences here – especially when Dropbox and Google Drive both use two different approaches (see first answer and comments below): DropBox is using the path while Google Drive is using IDs.

Best Answer

You may wish to look into Google Drive API's way of handling this. In their case, paths are abstractions and files are selected by id.

A file is a resource. It can belong to a folder, so it has a 'Parents' property. It can belong to multiple folders (either as a shared item, or to simulate soft-links), so it can have multiple 'Parents'. It can be moved to a different "folder", in which case the actual resource does not change location -- its parents property is just changed. The filename is not used in the URI, because an id is unique and a string can have odd characters.

Related Topic