|
| 1 | +--- |
| 2 | +title: Write an OpenAPI specification |
| 3 | +--- |
| 4 | + |
| 5 | +:::info |
| 6 | + |
| 7 | +Algolia's API specs follow the [OpenAPI specification, version 3.1](https://spec.openapis.org/oas/v3.1.0). |
| 8 | + |
| 9 | +Starting from an existing specification **really eases the contribution process and is recommended** (e.g. [search](https://github.com/algolia/api-clients-automation/blob/main/specs/search/)) |
| 10 | + |
| 11 | +**Do not edit the files in [`specs/bundled`](https://github.com/algolia/api-clients-automation/blob/main/specs/bundled)** they are the generated from the manually written specifications, and only used to generate API clients. |
| 12 | + |
| 13 | +::: |
| 14 | + |
| 15 | +:::caution |
| 16 | + |
| 17 | +A specification is used to generate the API client, its tests and code snippets, but is most importantly served by [the Algolia public documentation](https://www.algolia.com/doc/api-reference/rest-api/), please read our [API Documentation guidelines](./api-documentation-guidelines.md) to properly document your specification. |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +# Structure your specification |
| 22 | + |
| 23 | +Each API specification follows a consistent structure. |
| 24 | + |
| 25 | +## `specs/common/` |
| 26 | + |
| 27 | +This [common directory](https://github.com/algolia/api-clients-automation/blob/main/specs/common/) contains schemas and parameters that are common to multiple Algolia APIs, for example: search parameters or indexName definitions. |
| 28 | + |
| 29 | +## `specs/<apiName>/` |
| 30 | + |
| 31 | +Each API must be contained in its own directory, for example: [the Search API](https://github.com/algolia/api-clients-automation/blob/main/specs/search/). |
| 32 | + |
| 33 | +### `specs/<apiName>/spec.yml` |
| 34 | + |
| 35 | +This file is the main entrypoint of your specification and should describe your API, including the `servers`, `securitySchemes` and `paths` properties. |
| 36 | + |
| 37 | +### `specs/<apiName>/common/` |
| 38 | + |
| 39 | +This directory contains schemas and parameters that are common to **your API**. |
| 40 | +For schemas that are shared between multiple APIs please use the global [`specs/common`](#specscommon) directory. |
| 41 | + |
| 42 | +### `specs/<apiName>/paths/` |
| 43 | + |
| 44 | +This directory contains the descriptions of the **endpoints** of your API. |
| 45 | +The paths themselves are defined in the [`spec.yml`](#specsapinamespecyml) file. |
| 46 | + |
| 47 | +#### `specs/<apiName>/paths/<operation>.yml` |
| 48 | + |
| 49 | +Operations are endpoints combined with an HTTP method (`GET`, `POST`, etc.). |
| 50 | +Each operation must have a unique `operationID` property. |
| 51 | +Operations for the same endpoint may share the same file, for example, the `GET` and `DELETE` request for the same endpoint. |
| 52 | + |
| 53 | +##### Filenames |
| 54 | + |
| 55 | +Follow these conventions: |
| 56 | + |
| 57 | +- If the file only contains one operation, use `<operationId>.yml` as filename. |
| 58 | +- If the file contains multiple operations, use a more generic name, for example [`rule.yml`](https://github.com/algolia/api-clients-automation/blob/main/specs/search/paths/rules/rule.yml) for the `GET`, `PUT`, and `DELETE` request for a rule. |
| 59 | + |
| 60 | +##### Summaries and descriptions |
| 61 | + |
| 62 | +Every operation must have a `summary` and `description` property (they will be used in the generated API clients, and the Algolia documentation). |
| 63 | +For information about documenting operations, see [Operation summaries](/docs/add-a-new-api/api-documentation-guidelines#operation-summaries) and [Operation descriptions](./api-documentation-guidelines.md#operation-descriptions). |
| 64 | + |
| 65 | +##### Access Control Lists (ACL) |
| 66 | + |
| 67 | +Each operation should include an `x-acl` property |
| 68 | +to document the ACL required to make the request. |
| 69 | + |
| 70 | +The `x-acl` property is an array of strings, the allowed values are: `search`, `browse`, `addObject`, `deleteObject`, `listIndexes`, `deleteIndex`, `settings`, `editSettings`, `analytics`, `recommendation`, `usage`, `logs`, `setUnretrievableAttributes`, `admin`. |
| 71 | +For operations that require the admin API key, use `admin` |
| 72 | + |
| 73 | +##### Complex objects |
| 74 | + |
| 75 | +The following objects must not be inlined, but referenced with `$ref`: |
| 76 | + |
| 77 | +- Nested arrays |
| 78 | +- Nested objects |
| 79 | +- `oneOf` |
| 80 | +- `allOf` |
| 81 | +- `enum` |
| 82 | + |
| 83 | +This is required for accurate naming of the generated code objects. |
| 84 | +It also improves the readability of the specs. |
| 85 | + |
| 86 | +##### Properties and parameters |
| 87 | + |
| 88 | +- Create separate objects and reference them for [complex objects](#complex-objects). |
| 89 | +- The `format` parameter for strings isn't supported. |
| 90 | +- For **nullable properties**, use the following syntax: |
| 91 | + |
| 92 | + ```yaml |
| 93 | + nullableProp: |
| 94 | + default: null |
| 95 | + oneOf: |
| 96 | + - type: string |
| 97 | + description: Some value |
| 98 | + - type: 'null' |
| 99 | + description: The single quotes are required. |
| 100 | + ``` |
| 101 | +
|
| 102 | +For information about documenting properties and parameters, see [Properties and parameters](/docs/add-a-new-api/api-documentation-guidelines#properties-and-parameter-descriptions). |
| 103 | +
|
| 104 | +## CLI Commands |
| 105 | +
|
| 106 | +Use the CLI to generate build your specs: |
| 107 | +
|
| 108 | +- [Commands for working with specs](/docs/CLI/build-commands) |
| 109 | +
|
| 110 | +## Troubleshooting |
| 111 | +
|
| 112 | +### Explicit names for request bodies |
| 113 | +
|
| 114 | +> [Detailed issue](https://github.com/algolia/api-clients-automation/issues/891) |
| 115 | +
|
| 116 | +In some cases, the generated name for the `requestBody` property might be wrong. |
| 117 | +This can happen in these cases: |
| 118 | + |
| 119 | +- The type is too complex or too broad to be correctly generated, |
| 120 | + for example, [an object with `additionalProperties`](https://github.com/algolia/api-clients-automation/tree/main/specs/search/paths/objects/partialUpdate.yml#L24-L33). |
| 121 | + |
| 122 | +- The type is an alias of its model, |
| 123 | + for example, [a list of `model`](https://github.com/algolia/api-clients-automation/tree/main/specs/search/paths/rules/saveRules.yml#L12-L20). |
| 124 | + |
| 125 | +To provide a name for the request body, add the [`x-codegen-request-body-name`](https://openapi-generator.tech/docs/swagger-codegen-migration/#body-parameter-name) property to the root of the operation's spec file. |
| 126 | + |
| 127 | +For an example, see [pull request #896](https://github.com/algolia/api-clients-automation/pull/896). |
| 128 | + |
| 129 | +### Send additional options to the templates |
| 130 | + |
| 131 | +To send additional information to the generators, |
| 132 | +you can add properties starting with `x-` to the root level of your spec. |
| 133 | +These are available in the templates as part of the `vendorExtensions` object. |
| 134 | + |
| 135 | +For an example, see [`search.yml`](https://github.com/algolia/api-clients-automation/blob/main/specs/search/paths/search/search.yml#L5-L7) |
0 commit comments