Skip to content

Commit 995a4b1

Browse files
authored
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.
1 parent b62f9e3 commit 995a4b1

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

develop-docs/backend/api/design.mdx

+39-41
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Use the following guidelines for naming resources and their collections:
3131

3232
- **Do** use lowercase and hyphenated collection names, e.g. `commit-files`.
3333
- **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`.
3636

3737
Standard path parameters that should be shortened in routes:
3838

@@ -42,8 +42,8 @@ Standard path parameters that should be shortened in routes:
4242

4343
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:
4444

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}/`.
4747
- **Do not** expose endpoints which require `org` as a query parameter (it should always be a path parameter).
4848

4949
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:
5757

5858
**Do not** exceed three levels of resource nesting.
5959

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.
6161

6262
Here are some possible urls for values with this resource hierarchy: organization -> project -> tag -> value:
6363

64-
- 👍 `/projects/\{org}/\{project}/tags/\{tag}/values`
65-
- 👎 `/organizations/\{org}/projects/\{project}/tags/\{tag}/values/`
64+
- 👍 `/projects/{org}/{project}/tags/{tag}/values`
65+
- 👎 `/organizations/{org}/projects/{project}/tags/{tag}/values/`
6666
- 👎 `/values/`
6767

6868
Hierarchy here does not necessarily mean that one collection belongs to a parent collection, it simply implies a relationship. For example:
6969

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
7272

7373
## Parameter Design
7474

7575
- **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"}`.
7777

7878
For consistency, we also try to re-use well known parameters across endpoints.
7979

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`.
8383
- **Do** use `cursor` for pagination.
8484

8585
### Resource Identifiers
8686

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"}`).
8888

8989
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.
9090

@@ -119,24 +119,24 @@ POST /resources/{id}
119119

120120
### Batch Operations
121121

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.
123123

124124
Let's say for example we have an endpoint that mutates an issue:
125125

126126
```
127-
POST /api/0/organizations/:org/issues/:issue/
127+
POST /api/0/organizations/{org}/issues/{issue}/
128128
```
129129

130130
When designing a batch interface, we simply expose it on the collection instead of the individual resource:
131131

132132
```
133-
POST /api/0/organizations/:org/issues/
133+
POST /api/0/organizations/{org}/issues/
134134
```
135135

136136
You may also need to expose selectors on batch resources, which can be done through normal request parameters:
137137

138138
```
139-
POST /api/0/organizations/:org/issues/
139+
POST /api/0/organizations/{org}/issues/
140140
{
141141
"issues": [1, 2, 3]
142142
}
@@ -166,7 +166,7 @@ Here are some examples of how to use standard methods to represent complex tasks
166166

167167
**Retrieve statistics for a resource**
168168

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:
170170

171171
```
172172
GET /api/0/projects/{project}/
@@ -182,7 +182,7 @@ In some cases this will be returned as part of an HTTP header, specifically for
182182

183183
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/).
184184

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`
186186
- **Do not** create dedicated routes for these behaviors.
187187

188188
## Responses
@@ -191,13 +191,13 @@ Each response object returned from an API should be a serialized version of the
191191

192192
Some guidelines around the shape of responses:
193193

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": []}`).
197197
- **Do not** return custom objects. **Do** use a `Serializer` to serialize the resource.
198198
- **Do** return the smallest amount of data necessary to represent the resource.
199199

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:
201201

202202
- **Do** return resource identifiers (even numbers) as strings.
203203
- **Do** return decimals as strings.
@@ -222,7 +222,7 @@ Whereas our guidelines state it should be nested:
222222
GET /api/0/projects/{project}/
223223
{
224224
"project": {
225-
"id": 5,
225+
"id": "5",
226226
"name": "foo",
227227
...
228228
}
@@ -273,13 +273,13 @@ GET /api/0/projects/{project}/teams
273273
[
274274
{
275275
"id": 1,
276-
"name": "Team 1",
277-
"slug": "team1",
276+
"name": "Team 1",
277+
"slug": "team1",
278278
},
279-
{
279+
{
280280
"id": 2,
281-
"name": "Team 2",
282-
"slug": "team2",
281+
"name": "Team 2",
282+
"slug": "team2",
283283
}
284284
]
285285

@@ -297,17 +297,11 @@ GET /api/0/projects/{project}/
297297
"id": 5,
298298
"name": "foo",
299299
"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+
]
311305
}
312306
}
313307
```
@@ -330,7 +324,9 @@ This is typically only needed if the endpoint is already public and we do not wa
330324
>> 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.
331325
332326
Paginating responses is a [standard practice for APIs](https://google.aip.dev/158), which Sentry follows.
327+
333328
We've seen an example of a `List` endpoint above; these endpoints have two tell-tale signs:
329+
334330
```json
335331
GET /api/0/projects/{project}/teams
336332
[
@@ -347,12 +343,14 @@ GET /api/0/projects/{project}/teams
347343
]
348344

349345
```
346+
350347
1. The endpoint returns an array, or multiple, objects instead of just one.
351348
2. The endpoint can sometimes end in a plural (s), but more importantly, it does __not__ end in an identifier (`*_slug`, or `*_id`).
352349

353350
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.
354351
`self.paginate` is the standardized way we paginate at Sentry, and it helps us with unification of logging and monitoring.
355352
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:
353+
356354
```python
357355
def get(self, request: Request) -> Response:
358356
queryset = ApiApplication.objects.filter(

0 commit comments

Comments
 (0)