mattyh88
mattyh88

Reputation: 1645

How to instruct REST API to include additional data for given resource

I've implemented a GET request as followed:

https://api.com/invoices?filter[status]=paid&include=client

Current situation: Fetch invoices with status 'paid' & include client data for each invoice record

Now, I'd like to instruct my API to include additional data for each invoice record. This additional data is not really related to the invoice. (for example a log record of the last login of the client)

Desired: What are the best practices here? Which query param should I add according to industry standards, to instruct the request to include this additional data, as I wouldn't like to execute a query per invoice to fetch the last login for the client

Upvotes: 1

Views: 1585

Answers (2)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57257

How to instruct REST API to include additional data for given resource

Spelling check; as far as REST is concerned, you aren't asking for a given resource with additional data, you are asking for a different resource. There's no particular reason that this new resource needs an identifier related to the existing one.

What are the best practices here?

Think about how you would do it with a web site.

The URI would be created by presenting a form to the user, with input elements to allow the client to specify the values in the query string. In other words, the form is playing the role of a URI Template.

So to give the consumer additional control, you would include a new input control in the form - it might be a free text field, or it might be something like a list control which enumerates specific possible values.

Which query param should I add according to industry standards

There are a couple possibilities.

You could review the list of IANA Link Relations, to see if there is a close match. Technically, link relations aren't query parameters. But what this list does is match spellings with semantics, which is what you really want: is there a common understanding of foobar that you can leverage for your own needs.

Another possibility is to look in resources like schema.org, which again has lots of interesting mappings between spelling and semantics.

I happen to know off the top of my head that the EventStore API uses embed with different values to allow the client to specify that it wants to access resources with richer information; but to the best of my knowledge that choice was arbitrary, not based on any real "industry standard".

Upvotes: 1

Kuilo Skio
Kuilo Skio

Reputation: 1

how about this, example :

// your code here

$result; // your variable that will send by your API

$user = User::all(); // your additional query to be included to your response API

// additional data example all user data $result['user'] = $user;

Upvotes: 0

Related Questions