Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Informative vs unique generated ID in REST API

Tags:

rest

Designing a RESTful API. I have two ways of identifying resources (person data). Either by the unique ID generated by the database, or by a social security number (SSN), entered for each person. The SSN is supposedly unique, though can be changed.

Using the ID would be most convenient for me, since it is guaranteed to be unique, and does not change. Hence the URL for the resource, also always stays the same:

GET /persons/12

{
  "name": Morgan
  "ssn": "840212-3312"
}

The argument for using SSN, is that it is more informative and understandable by API clients. SSN is also used more in surrounding systems:

GET /persons/840212-3321

{
  "name": Morgan
  "id": "12"
}

So the question is: Should I go with the first approach, and avoid some implementation headaches where the SSN may change. And maybe provide some helper method that converts from SSN to ID?

Or go with the second approach. Providing a more informative API. Though having to deal with some not so RESTful strangeness where URL:s might change due to SSN changes?

like image 990
Morgan Bengtsson Avatar asked Feb 11 '13 15:02

Morgan Bengtsson


People also ask

What is ID in REST API?

Your rest-api-id is the identifier before 'execute-api' in your endpoint URL.

How do you generate unique identifiers?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.

What is REST API example?

For example, a REST API would use a GET request to retrieve a record, a POST request to create one, a PUT request to update a record, and a DELETE request to delete one. All HTTP methods can be used in API calls. A well-designed REST API is similar to a website running in a web browser with built-in HTTP functionality.

What is RESTful API?

RESTful API is an interface that two computer systems use to exchange information securely over the internet. Most business applications have to communicate with other internal and third-party applications to perform various tasks.


2 Answers

URL design is a personal choice. But to give you some more examples which differ from those Ray has already provided, I will give you some of my own.

I have a user account resource and allow access via both URIs:

/users/12

and

/users/morgan

where the numerical value is an auto_incremented ID, and the alphabetic value is a unique username on the system specified by the user. these resources are uncachable so I do not bother about canonicalisation, however the /users page links to the alphabetic forms.

No other resources on my system have two unique fields, so are referred to by IDs, /jobs/123, /quotations/456.

As you can see, I prefer plural URI segments ;-)
I think of "job 123" as being from the "jobs" collection, so it seems logical to have a "jobs" resource, with subresources for each job.

You do not need to have a separate /search/ area for performing searches, I think it would be cleaner to apply your search criteria to the collection resource directly:

/people?ssn=123456-7890  (people with SSN matching/containing "123456-7890")
/people?name=morgan      (people who's name is/contains "Morgan")

I have something similar, but use only the first letter as a filter:

/sites?alpha=f

Lists all sites beginning with F. You can think of it as a filter, or as a search criteria, those terms are just different sides of the same coin.

like image 143
Nicholas Shanks Avatar answered Oct 20 '22 09:10

Nicholas Shanks


Good to see someone taking time to think about their Resource urls!

I would make a Url with the unique id to provide resource to a single user. Like:

http://api.mysite.com/person/12/

Where 12 is your unique ID. Note that I also prefer the singular 'person'....

Regardless, the url should return:

{
  "ssn":  "840212-3312"
  "name": "Morgan"
  "id": "12"
}

However, I would also create a general search URL that returns a list of users that match the parameters (either a json array or whatever format you need). You can specify search parameters as get params like this:

 http://api.mysite.com/person/search/?ssn=840212-3312

Or

 http://api.mysite.com/person/search/?name=Morgan

These would return something like this for a single search hit--note it's an array, not a single item like the unique id url that points directly to a single user.

[{
  "ssn":  "840212-3312"
  "name": "Morgan"
  "id": "12"
}]

This search could then be later augmented for other search criteria. You might only return the unique id's via the search Url--you could always make a request to the unique id url once you've got it from the search...

like image 31
Ray Avatar answered Oct 20 '22 09:10

Ray