You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make the API Design doc a bit more consistent (#13511)
In particular, this removes a bunch of stray backslashes, like `\{`.
It also uses more consistent formatting of paths, related to slashes and
placeholders.
Copy file name to clipboardExpand all lines: develop-docs/backend/api/design.mdx
+39-41
Original file line number
Diff line number
Diff line change
@@ -31,8 +31,8 @@ Use the following guidelines for naming resources and their collections:
31
31
32
32
-**Do** use lowercase and hyphenated collection names, e.g. `commit-files`.
33
33
-**Do** use plural collection names. Avoid using uncountable words because the user can't know whether the GET returns one item or a list.
34
-
-**Do** use `snake_case` for path parameters. e.g. `tags/\{tag_name}/`.
35
-
-**Do** consistently shorten parameters that are excessively long when the term will unambiguous. e.g. `organization` -> `org`.
34
+
-**Do** use `snake_case` for path parameters. e.g. `tags/{tag_name}/`.
35
+
-**Do** consistently shorten parameters that are excessively long when the term is unambiguous. e.g. `organization` -> `org`.
36
36
37
37
Standard path parameters that should be shortened in routes:
38
38
@@ -42,8 +42,8 @@ Standard path parameters that should be shortened in routes:
42
42
43
43
Information in Sentry is typically constrained by tenants. That is, almost all information is scoped to an organization. All endpoints which query customer data **must** be scoped to an organization:
44
44
45
-
-**Do** prefix resource organizations collections with `organizations/\{org}`.
46
-
-**Do** prefix resource project collections with `projects/\{org}/\{project}`.
45
+
-**Do** prefix organization resource collections with `/organizations/{org}/`.
46
+
-**Do** prefix project resource collections with `/projects/{org}/{project}/`.
47
47
-**Do not** expose endpoints which require `org` as a query parameter (it should always be a path parameter).
48
48
49
49
Knowing when to choose which constraint to couple an endpoint to will be based on the purpose of an endpoint. For example, if an endpoint is only ever going to be used to query data for a single project, it should be prefixed with `/projects/{org}/{project}/things`. If an endpoint would need to exist to query multiple projects (which is common with cross-project queries), you likely should expose it as `/organizations/{org}/things`, and expose a query param to filter on the project(s).
@@ -57,34 +57,34 @@ Exceptions to these rules include:
57
57
58
58
**Do not** exceed three levels of resource nesting.
59
59
60
-
Nesting resources such as `/organizations/\{org}/projects/`, is **preferred** over flattened resources like `/0/projects/`. This improves readability and exposes a natural understanding of resource hierarchy and relationships. However, nesting can make URLs too long and hard to use. Sentry uses 3-level nesting as a hybrid solution.
60
+
Nesting resources such as `/organizations/{org}/projects/`, is **preferred** over flattened resources like `/0/projects/`. This improves readability and exposes a natural understanding of resource hierarchy and relationships. However, nesting can make URLs too long and hard to use. Sentry uses 3-level nesting as a hybrid solution.
61
61
62
62
Here are some possible urls for values with this resource hierarchy: organization -> project -> tag -> value:
Hierarchy here does not necessarily mean that one collection belongs to a parent collection, it simply implies a relationship. For example:
69
69
70
-
-`projects/\{project_identifier}/teams/` refers to the **teams** that have been added to specific project
71
-
-`teams/\{team_identifier}/projects/` refers to the **projects** a specific team has been added to
70
+
-`/projects/{project_identifier}/teams/` refers to the **teams** that have been added to specific project
71
+
-`/teams/{team_identifier}/projects/` refers to the **projects** a specific team has been added to
72
72
73
73
## Parameter Design
74
74
75
75
-**Do** use `camelCase` for query params and request body params. e.g. `/foo/?userId=123`.
76
-
-**Do** use `camelCase` for all response attributes. e.g. `\{userId: "123"}`.
76
+
-**Do** use `camelCase` for all response attributes. e.g. `{userId: "123"}`.
77
77
78
78
For consistency, we also try to re-use well known parameters across endpoints.
79
79
80
-
-**Do** use `sortBy` for sorting. e.g. `sortBy=-dateCreated`.
81
-
-**Do** use `orderBy` for ordering. e.g. `orderBy=asc` or `orderBy=desc`.
82
-
-**Do** use `limit` for limiting the number of results returned. e.g. `limit=10`.
80
+
-**Do** use `sortBy` for sorting. e.g. `?sortBy=-dateCreated`.
81
+
-**Do** use `orderBy` for ordering. e.g. `?orderBy=asc` or `?orderBy=desc`.
82
+
-**Do** use `limit` for limiting the number of results returned. e.g. `?limit=10`.
83
83
-**Do** use `cursor` for pagination.
84
84
85
85
### Resource Identifiers
86
86
87
-
Identifiers exist both within the route (`/organizations/\{organization}/projects/`) as well as within other parameters such as query strings (`organization=123`) and request bodies (`\{organization: "123"}`).
87
+
Identifiers exist both within the route (`/organizations/{organization}/projects/`) as well as within other parameters such as query strings (`?organization=123`) and request bodies (`{organization: "123"}`).
88
88
89
89
The most important concern here is to ensure that a single identifier is exposed to key to resources. For example, it is preferred to use `organization` and accept both `organization_id` and `organization_slug` as valid identifiers.
90
90
@@ -119,24 +119,24 @@ POST /resources/{id}
119
119
120
120
### Batch Operations
121
121
122
-
Resources can get complicated when you need to expose batch operations vs single resource operations. For batch operations it it is preferred to expose them as a `POST` request on the collection when possible.
122
+
Resources can get complicated when you need to expose batch operations vs single resource operations. For batch operations it is preferred to expose them as a `POST` request on the collection when possible.
123
123
124
124
Let's say for example we have an endpoint that mutates an issue:
125
125
126
126
```
127
-
POST /api/0/organizations/:org/issues/:issue/
127
+
POST /api/0/organizations/{org}/issues/{issue}/
128
128
```
129
129
130
130
When designing a batch interface, we simply expose it on the collection instead of the individual resource:
131
131
132
132
```
133
-
POST /api/0/organizations/:org/issues/
133
+
POST /api/0/organizations/{org}/issues/
134
134
```
135
135
136
136
You may also need to expose selectors on batch resources, which can be done through normal request parameters:
137
137
138
138
```
139
-
POST /api/0/organizations/:org/issues/
139
+
POST /api/0/organizations/{org}/issues/
140
140
{
141
141
"issues": [1, 2, 3]
142
142
}
@@ -166,7 +166,7 @@ Here are some examples of how to use standard methods to represent complex tasks
166
166
167
167
**Retrieve statistics for a resource**
168
168
169
-
The best approach here is to encoded it as an attribute in the resource:
169
+
The best approach here is to encode it as an attribute in the resource:
170
170
171
171
```
172
172
GET /api/0/projects/{project}/
@@ -182,7 +182,7 @@ In some cases this will be returned as part of an HTTP header, specifically for
182
182
183
183
Order and filtering should happen as part of list api query parameters. Here's a [good read](https://www.moesif.com/blog/technical/api-design/REST-API-Design-Filtering-Sorting-and-Pagination/).
184
184
185
-
-**Do** rely on `orderBy` and `sortBy`. e.g. `/api/0/issues/\{issue_id}/events?orderBy=-date`
185
+
-**Do** rely on `orderBy` and `sortBy`. e.g. `/api/0/issues/{issue_id}/events?orderBy=-date`
186
186
-**Do not** create dedicated routes for these behaviors.
187
187
188
188
## Responses
@@ -191,13 +191,13 @@ Each response object returned from an API should be a serialized version of the
191
191
192
192
Some guidelines around the shape of responses:
193
193
194
-
-**Do** use `camelCase` for all response attributes. e.g. `\{numCount: "123"}`.
195
-
-**Do** return a responses as a named resource (e.g. `\{"user": \{"id": "123"}}`).
196
-
-**Do** indicate collections using plural nouns (e.g. `\{"users": []}`).
194
+
-**Do** use `camelCase` for all response attributes. e.g. `{"numCount": "123"}`.
195
+
-**Do** return a responses as a named resource (e.g. `{"user": {"id": "123"}}`).
196
+
-**Do** indicate collections using plural nouns (e.g. `{"users": []}`).
197
197
-**Do not** return custom objects. **Do** use a `Serializer` to serialize the resource.
198
198
-**Do** return the smallest amount of data necessary to represent the resource.
199
199
200
-
Additionally because JavaScript is a primary consumer, be mindful of the restrictions are things like numbers. Generally speaking:
200
+
Additionally because JavaScript is a primary consumer, be mindful of the restrictions on things like numbers. Generally speaking:
201
201
202
202
-**Do** return resource identifiers (even numbers) as strings.
203
203
-**Do** return decimals as strings.
@@ -222,7 +222,7 @@ Whereas our guidelines state it should be nested:
222
222
GET /api/0/projects/{project}/
223
223
{
224
224
"project": {
225
-
"id": 5,
225
+
"id": "5",
226
226
"name": "foo",
227
227
...
228
228
}
@@ -273,13 +273,13 @@ GET /api/0/projects/{project}/teams
273
273
[
274
274
{
275
275
"id": 1,
276
-
"name": "Team 1",
277
-
"slug": "team1",
276
+
"name": "Team 1",
277
+
"slug": "team1",
278
278
},
279
-
{
279
+
{
280
280
"id": 2,
281
-
"name": "Team 2",
282
-
"slug": "team2",
281
+
"name": "Team 2",
282
+
"slug": "team2",
283
283
}
284
284
]
285
285
@@ -297,17 +297,11 @@ GET /api/0/projects/{project}/
297
297
"id": 5,
298
298
"name": "foo",
299
299
"stats": {
300
-
"24h": [
301
-
[
302
-
1629064800,
303
-
27
304
-
],
305
-
[
306
-
1629068400,
307
-
24
308
-
],
309
-
...
310
-
]
300
+
"24h": [
301
+
[1629064800, 27],
302
+
[1629068400, 24],
303
+
...
304
+
]
311
305
}
312
306
}
313
307
```
@@ -330,7 +324,9 @@ This is typically only needed if the endpoint is already public and we do not wa
330
324
>> APIs often need to provide collections of data, most commonly in the `List` standard method. However, collections can be arbitrarily sized, and tend to grow over time, increasing lookup time as well as the size of the responses being sent over the wire. This is why it's important for collections to be paginated.
331
325
332
326
Paginating responses is a [standard practice for APIs](https://google.aip.dev/158), which Sentry follows.
327
+
333
328
We've seen an example of a `List` endpoint above; these endpoints have two tell-tale signs:
329
+
334
330
```json
335
331
GET /api/0/projects/{project}/teams
336
332
[
@@ -347,12 +343,14 @@ GET /api/0/projects/{project}/teams
347
343
]
348
344
349
345
```
346
+
350
347
1. The endpoint returns an array, or multiple, objects instead of just one.
351
348
2. The endpoint can sometimes end in a plural (s), but more importantly, it does __not__ end in an identifier (`*_slug`, or `*_id`).
352
349
353
350
To paginate a response at Sentry, you can leverage the [`self.paginate`](https://github.com/getsentry/sentry/blob/24.2.0/src/sentry/api/base.py#L463-L476) method as part of your endpoint.
354
351
`self.paginate` is the standardized way we paginate at Sentry, and it helps us with unification of logging and monitoring.
355
352
You can find multiple [examples of this](https://github.com/getsentry/sentry/blob/24.2.0/src/sentry/api/endpoints/api_applications.py#L22-L33) in the code base. They'll look something like:
0 commit comments