|
| 1 | +# 5. REST API Reference |
| 2 | + |
| 3 | +The WordPress Abilities API provides REST endpoints that allow external systems to discover and execute abilities via HTTP requests. |
| 4 | + |
| 5 | +## User access |
| 6 | + |
| 7 | +Access to all Abilities REST API endpoints requires an authenticated user (see the [Authentication](#authentication) section). Access to execute individual Abilities is restricted based on the `permission_callback()` of the Ability. |
| 8 | + |
| 9 | +## Schema |
| 10 | + |
| 11 | +The Abilities API endpoints are available under the `/wp/v2/abilities` namespace. |
| 12 | + |
| 13 | +### Ability Object |
| 14 | + |
| 15 | +Abilities are represented in JSON with the following structure: |
| 16 | + |
| 17 | +```json |
| 18 | +{ |
| 19 | + "name": "my-plugin/get-site-info", |
| 20 | + "label": "Get Site Information", |
| 21 | + "description": "Retrieves basic information about the WordPress site.", |
| 22 | + "output_schema": { |
| 23 | + "type": "object", |
| 24 | + "properties": { |
| 25 | + "name": { |
| 26 | + "type": "string", |
| 27 | + "description": "Site name" |
| 28 | + }, |
| 29 | + "url": { |
| 30 | + "type": "string", |
| 31 | + "format": "uri", |
| 32 | + "description": "Site URL" |
| 33 | + } |
| 34 | + } |
| 35 | + }, |
| 36 | + "meta": {} |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## List Abilities |
| 41 | + |
| 42 | +### Definition |
| 43 | + |
| 44 | +`GET /wp/v2/abilities` |
| 45 | + |
| 46 | +### Arguments |
| 47 | + |
| 48 | +- `page` _(integer)_: Current page of the collection. Default: `1`. |
| 49 | +- `per_page` _(integer)_: Maximum number of items to return per page. Default: `50`, Maximum: `100`. |
| 50 | + |
| 51 | +### Example Request |
| 52 | + |
| 53 | +```bash |
| 54 | +curl https://example.com/wp-json/wp/v2/abilities |
| 55 | +``` |
| 56 | + |
| 57 | +### Example Response |
| 58 | + |
| 59 | +```json |
| 60 | +[ |
| 61 | + { |
| 62 | + "name": "my-plugin/get-site-info", |
| 63 | + "label": "Get Site Information", |
| 64 | + "description": "Retrieves basic information about the WordPress site.", |
| 65 | + "output_schema": { |
| 66 | + "type": "object", |
| 67 | + "properties": { |
| 68 | + "name": { |
| 69 | + "type": "string", |
| 70 | + "description": "Site name" |
| 71 | + }, |
| 72 | + "url": { |
| 73 | + "type": "string", |
| 74 | + "format": "uri", |
| 75 | + "description": "Site URL" |
| 76 | + } |
| 77 | + } |
| 78 | + }, |
| 79 | + "meta": {} |
| 80 | + } |
| 81 | +] |
| 82 | +``` |
| 83 | + |
| 84 | +## Retrieve an Ability |
| 85 | + |
| 86 | +### Definition |
| 87 | + |
| 88 | +`GET /wp/v2/abilities/(?P<namespace>[a-z0-9-]+)/(?P<ability>[a-z0-9-]+)` |
| 89 | + |
| 90 | +### Arguments |
| 91 | + |
| 92 | +- `namespace` _(string)_: The namespace part of the ability name. |
| 93 | +- `ability` _(string)_: The ability name part. |
| 94 | + |
| 95 | +### Example Request |
| 96 | + |
| 97 | +```bash |
| 98 | +curl https://example.com/wp-json/wp/v2/abilities/my-plugin/get-site-info |
| 99 | +``` |
| 100 | + |
| 101 | +### Example Response |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "name": "my-plugin/get-site-info", |
| 106 | + "label": "Get Site Information", |
| 107 | + "description": "Retrieves basic information about the WordPress site.", |
| 108 | + "output_schema": { |
| 109 | + "type": "object", |
| 110 | + "properties": { |
| 111 | + "name": { |
| 112 | + "type": "string", |
| 113 | + "description": "Site name" |
| 114 | + }, |
| 115 | + "url": { |
| 116 | + "type": "string", |
| 117 | + "format": "uri", |
| 118 | + "description": "Site URL" |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + "meta": {} |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Execute an Ability |
| 127 | + |
| 128 | +### Definition |
| 129 | + |
| 130 | +`POST /wp/v2/abilities/(?P<namespace>[a-z0-9-]+)/(?P<ability>[a-z0-9-]+)/run` |
| 131 | + |
| 132 | +### Arguments |
| 133 | + |
| 134 | +- `namespace` _(string)_: The namespace part of the ability name. |
| 135 | +- `ability` _(string)_: The ability name part. |
| 136 | +- `input` _(integer|number|boolean|string|array|object|null)_: Optional input data for the ability as defined by its input schema. |
| 137 | + |
| 138 | +### Example Request (No Input) |
| 139 | + |
| 140 | +```bash |
| 141 | +curl -X POST https://example.com/wp-json/wp/v2/abilities/my-plugin/get-site-info/run |
| 142 | +``` |
| 143 | + |
| 144 | +### Example Request (With Input) |
| 145 | + |
| 146 | +```bash |
| 147 | +curl -X POST \ |
| 148 | + -H "Content-Type: application/json" \ |
| 149 | + -d '{"input":{"option_name":"blogname","option_value":"New Site Name"}}' \ |
| 150 | + https://example.com/wp-json/wp/v2/abilities/my-plugin/update-option/run |
| 151 | +``` |
| 152 | + |
| 153 | +### Example Response (Success) |
| 154 | + |
| 155 | +```json |
| 156 | +{ |
| 157 | + "name": "My WordPress Site", |
| 158 | + "url": "https://example.com" |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### Example Response (Error) |
| 163 | + |
| 164 | +```json |
| 165 | +{ |
| 166 | + "code": "ability_invalid_permissions", |
| 167 | + "message": "Ability \"my-plugin/update-option\" does not have necessary permission.", |
| 168 | + "data": { |
| 169 | + "status": 403 |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## Authentication |
| 175 | + |
| 176 | +The Abilities API supports all WordPress REST API authentication methods: |
| 177 | + |
| 178 | +- Cookie authentication (same-origin requests) |
| 179 | +- Application passwords (recommended for external access) |
| 180 | +- Custom authentication plugins |
| 181 | + |
| 182 | +### Using Application Passwords |
| 183 | + |
| 184 | +```bash |
| 185 | +curl -u 'USERNAME:APPLICATION_PASSWORD' \ |
| 186 | + https://example.com/wp-json/wp/v2/abilities |
| 187 | +``` |
| 188 | + |
| 189 | +## Error Responses |
| 190 | + |
| 191 | +The API returns standard WordPress REST API error responses with these common codes: |
| 192 | + |
| 193 | +- `ability_missing_input_schema` – the ability requires input but none was provided. |
| 194 | +- `ability_invalid_input` - input validation failed according to the ability's schema. |
| 195 | +- `ability_invalid_permissions` - current user lacks permission to execute the ability. |
| 196 | +- `ability_invalid_output` - output validation failed according to the ability's schema. |
| 197 | +- `ability_invalid_execute_callback` - the ability's execute callback is not callable. |
| 198 | +- `rest_ability_not_found` - the requested ability is not registered. |
| 199 | +- `rest_ability_invalid_method` - the requested HTTP method is not allowed for executing the selected ability. |
| 200 | +- `rest_ability_cannot_execute` - the ability cannot be executed due to insufficient permissions. |
0 commit comments