Skip to content

Commit 8d453d3

Browse files
authored
docs: added details about expand, fields, and parameter types (medusajs#3325)
1 parent 8e5ebc9 commit 8d453d3

File tree

2 files changed

+339
-14
lines changed

2 files changed

+339
-14
lines changed

docs/api/admin-spec3-base.yaml

+178-7
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,185 @@ info:
1515
1616
In many endpoints you'll find an `expand` query parameter that can be passed to the endpoint. You can use the `expand` query parameter to unpack an entity's relations and return them in the response.
1717
18-
For example, when you list customers you can also retrieve their groups by passing to the `expand` query parameter the value `groups`.
18+
Please note that the relations you pass to `expand` replace any relations that are expanded by default in the request.
1919
20-
You can expand more than one relation by separating the relations in the `expand` query parameter with a comma. For example, to retrieve both the orders and the groups of a customer, pass to the `expand` query parameter the value `groups,orders`.
20+
### Expanding One Relation
2121
22-
Please note that the parameters you pass to `expand` replace any relations that are expanded by default.
22+
For example, when you retrieve products, you can retrieve their collection by passing to the `expand` query parameter the value `collection`:
23+
24+
```bash
25+
curl "http://localhost:9000/admin/products?expand=collection" \
26+
-H 'Authorization: Bearer {api_token}'
27+
```
28+
29+
### Expanding Multiple Relations
30+
31+
You can expand more than one relation by separating the relations in the `expand` query parameter with a comma.
32+
33+
For example, to retrieve both the variants and the collection of products, pass to the `expand` query parameter the value `variants,collection`:
34+
35+
```bash
36+
curl "http://localhost:9000/admin/products?expand=variants,collection" \
37+
-H 'Authorization: Bearer {api_token}'
38+
```
39+
40+
### Prevent Expanding Relations
41+
42+
Some requests expand relations by default. You can prevent that by passing an empty expand value to retrieve an entity without any extra relations.
43+
44+
For example:
45+
46+
```bash
47+
curl "http://localhost:9000/admin/products?expand" \
48+
-H 'Authorization: Bearer {api_token}'
49+
```
50+
51+
This would retrieve each product with only its properties, without any relations like `collection`.
2352
2453
## Selecting Fields
2554
2655
In many endpoints you'll find a `fields` query parameter that can be passed to the endpoint. You can use the `fields` query parameter to specify which fields in the entity should be returned in the response.
2756
57+
Please note that if you pass a `fields` query parameter, only the fields you pass in the value along with the `id` of the entity will be returned in the response.
58+
59+
Also, the `fields` query parameter does not affect the expanded relations. You'll have to use the `expand` parameter instead.
60+
61+
### Selecting One Field
62+
63+
For example, when you retrieve a list of products, you can retrieve only the titles of the products by passing `title` as a value to the `fields` query parameter:
64+
65+
```bash
66+
curl "http://localhost:9000/admin/products?fields=title" \
67+
-H 'Authorization: Bearer {api_token}'
68+
```
69+
70+
As mentioned above, the expanded relations such as `variants` will still be returned as they're not affected by the `fields` parameter.
71+
72+
You can ensure that only the `title` field is returned by passing an empty value to the `expand` query parameter. For example:
73+
74+
```bash
75+
curl "http://localhost:9000/admin/products?fields=title&expand" \
76+
-H 'Authorization: Bearer {api_token}'
77+
```
78+
79+
### Selecting Multiple Fields
80+
2881
You can pass more than one field by seperating the field names in the `fields` query parameter with a comma.
2982
30-
Only the fields you pass to `field` will be retrieved and returned in the response. Any fields that are returned by default will not be returned in this case. This does not affect relations.
83+
For example, to select the `title` and `handle` of products:
84+
85+
```bash
86+
curl "http://localhost:9000/admin/products?fields=title,handle" \
87+
-H 'Authorization: Bearer {api_token}'
88+
```
89+
90+
### Retrieve Only the ID
91+
92+
You can pass an empty `fields` query parameter to return only the ID of an entity. For example:
93+
94+
```bash
95+
curl "http://localhost:9000/admin/products?fields" \
96+
-H 'Authorization: Bearer {api_token}'
97+
```
98+
99+
You can also pair with an empty `expand` query parameter to ensure that the relations aren't retrieved as well. For example:
100+
101+
```bash
102+
curl "http://localhost:9000/admin/products?fields&expand" \
103+
-H 'Authorization: Bearer {api_token}'
104+
```
105+
106+
## Query Parameter Types
107+
108+
This section covers how to pass some common data types as query parameters. This is useful if you're sending requests to the API endpoints and not using our JS Client. For example, when using cURL or Postman.
109+
110+
### Strings
111+
112+
You can pass a string value in the form of `<parameter_name>=<value>`.
113+
114+
For example:
115+
116+
```bash
117+
curl "http://localhost:9000/admin/products?title=Shirt" \
118+
-H 'Authorization: Bearer {api_token}'
119+
```
120+
121+
If the string has any characters other than letters and numbers, you must encode them.
122+
123+
For example, if the string has spaces, you can encode the space with `+` or `%20`:
124+
125+
```bash
126+
curl "http://localhost:9000/admin/products?title=Blue%20Shirt" \
127+
-H 'Authorization: Bearer {api_token}'
128+
```
129+
130+
You can use tools like [this one](https://www.urlencoder.org/) to learn how a value can be encoded.
131+
132+
### Integers
133+
134+
You can pass an integer value in the form of `<parameter_name>=<value>`.
135+
136+
For example:
137+
138+
```bash
139+
curl "http://localhost:9000/admin/products?offset=1" \
140+
-H 'Authorization: Bearer {api_token}'
141+
```
142+
143+
### Boolean
144+
145+
You can pass a boolean value in the form of `<parameter_name>=<value>`.
146+
147+
For example:
148+
149+
```bash
150+
curl "http://localhost:9000/admin/products?is_giftcard=true" \
151+
-H 'Authorization: Bearer {api_token}'
152+
```
153+
154+
### Date and DateTime
155+
156+
You can pass a date value in the form `<parameter_name>=<value>`. The date must be in the format `YYYY-MM-DD`.
31157
32-
For example, to only select the `title` and `description` fields of a product, pass to the `fields` query parameter the value `title,description`.
158+
For example:
159+
160+
```bash
161+
curl -g "http://localhost:9000/admin/products?created_at[lt]=2023-02-17" \
162+
-H 'Authorization: Bearer {api_token}'
163+
```
164+
165+
You can also pass the time using the format `YYYY-MM-DDTHH:MM:SSZ`. Please note that the `T` and `Z` here are fixed.
166+
167+
For example:
168+
169+
```bash
170+
curl -g "http://localhost:9000/admin/products?created_at[lt]=2023-02-17T07:22:30Z" \
171+
-H 'Authorization: Bearer {api_token}'
172+
```
173+
174+
### Array
175+
176+
Each array value must be passed as a separate query parameter in the form `<parameter_name>[]=<value>`. You can also specify the index of each parameter in the brackets `<parameter_name>[0]=<value>`.
177+
178+
For example:
179+
180+
```bash
181+
curl -g "http://localhost:9000/admin/products?sales_channel_id[]=sc_01GPGVB42PZ7N3YQEP2WDM7PC7&sales_channel_id[]=sc_234PGVB42PZ7N3YQEP2WDM7PC7" \
182+
-H 'Authorization: Bearer {api_token}'
183+
```
184+
185+
Note that the `-g` parameter passed to `curl` disables errors being thrown for using the brackets. Read more [here](https://curl.se/docs/manpage.html#-g).
186+
187+
### Object
188+
189+
Object parameters must be passed as separate query parameters in the form `<parameter_name>[<key>]=<value>`.
190+
191+
For example:
192+
193+
```bash
194+
curl -g "http://localhost:9000/admin/products?created_at[lt]=2023-02-17&created_at[gt]=2022-09-17" \
195+
-H 'Authorization: Bearer {api_token}'
196+
```
33197
34198
## Pagination
35199
@@ -41,11 +205,18 @@ info:
41205
42206
You can use the `offset` query parameter to change between pages. For example, if the limit is 50, at page 1 the offset should be 0; at page 2 the offset should be 50, and so on.
43207
208+
For example, to limit the number of products returned in the List Products endpoint:
209+
210+
```bash
211+
curl "http://localhost:9000/admin/products?limit=5" \
212+
-H 'Authorization: Bearer {api_token}'
213+
```
214+
44215
### Response Fields
45216
46-
In listing fields, aside from the entities retrieved, there are three pagination-related fields returned: `count`, `limit`, and `offset`.
217+
In the response of listing endpoints, aside from the entities retrieved, there are three pagination-related fields returned: `count`, `limit`, and `offset`.
47218
48-
Similarly to the query parameters, `limit` is the maximum number of items that can be returned in the response, and `field` is the number of items that were skipped before the entities in the result.
219+
Similar to the query parameters, `limit` is the maximum number of items that can be returned in the response, and `field` is the number of items that were skipped before the entities in the result.
49220
50221
`count` is the total number of available items of this entity. It can be used to determine how many pages are there.
51222

0 commit comments

Comments
 (0)