@@ -177,6 +177,146 @@ The Monaco Editor automatically replaces `.rex-code` textareas in:
1771773 . Results show filename, line number, context
1781784 . Click result to open file at specific line
179179
180+ ### API (api AddOn)
181+ When the ** api** addon is installed and active, the code addon registers these endpoints automatically:
182+
183+ ** Important prerequisite:** File operations (` /api/code/files ` and ` /api/code/file ` ) only work when ** Code → Settings → File-Browser aktivieren** is enabled. If disabled, the API returns ` 403 ` .
184+
185+ - ` GET /api/code/capabilities ` (Bearer token scope: ` code/capabilities ` )
186+ - ` GET /api/code/files ` (Bearer token scope: ` code/files/list ` , query: ` path ` )
187+ - ` POST /api/code/files ` (Bearer token scope: ` code/files/create ` , JSON body: ` path ` , ` name ` , ` type=file|folder ` )
188+ - ` GET /api/code/file ` (Bearer token scope: ` code/file/read ` , query: ` path ` )
189+ - ` PUT/PATCH /api/code/file ` (Bearer token scope: ` code/file/update ` , JSON body: ` path ` , ` content ` )
190+ - ` DELETE /api/code/file ` (Bearer token scope: ` code/file/delete ` , query: ` path ` )
191+ - ` GET /api/backend/code/capabilities ` (Backend session/cookie auth)
192+ - ` GET /api/backend/code/files ` (Backend session/cookie auth)
193+ - ` POST /api/backend/code/files ` (Backend session/cookie auth)
194+ - ` GET/PUT/PATCH/DELETE /api/backend/code/file ` (Backend session/cookie auth)
195+
196+ Response includes:
197+ - addon metadata (` addon ` , ` version ` )
198+ - file browser status (` file_browser_enabled ` )
199+ - editable file formats (` allowed_extensions ` )
200+ - excluded directories (` excluded_directories ` )
201+
202+ ` GET /api/code/files ` returns the current directory entries (folders + allowed file types).
203+
204+ ` POST /api/code/files ` creates a new file or folder. Example body:
205+
206+ ``` json
207+ {
208+ "path" : " redaxo/src/addons/code" ,
209+ "name" : " example.csv" ,
210+ "type" : " file"
211+ }
212+ ```
213+
214+ ` GET /api/code/file ` returns file metadata and ` content ` for an allowed file.
215+
216+ ` PUT /api/code/file ` updates file content. Example body:
217+
218+ ``` json
219+ {
220+ "path" : " redaxo/src/addons/code/example.csv" ,
221+ "content" : " id;name\n 1;Demo\n "
222+ }
223+ ```
224+
225+ ` DELETE /api/code/file ` deletes an allowed non-protected file.
226+
227+ #### Curl examples (copy & paste)
228+
229+ Note: These examples require that the file browser is enabled in the code addon settings.
230+
231+ ``` bash
232+ BASE=' https://localhost:8443'
233+ TOKEN=' YOUR_TOKEN'
234+ ```
235+
236+ List directory entries:
237+
238+ ``` bash
239+ curl -k -sS -G \
240+ -H " Authorization: Bearer $TOKEN " \
241+ --data-urlencode " path=redaxo/src/addons/code" \
242+ " $BASE /api/code/files"
243+ ```
244+
245+ Create a new CSV file:
246+
247+ ``` bash
248+ curl -k -sS -X POST \
249+ -H " Authorization: Bearer $TOKEN " \
250+ -H " Content-Type: application/json" \
251+ --data ' {"path":"redaxo/src/addons/code","name":"api_demo.csv","type":"file"}' \
252+ " $BASE /api/code/files"
253+ ```
254+
255+ Read file content:
256+
257+ ``` bash
258+ curl -k -sS -G \
259+ -H " Authorization: Bearer $TOKEN " \
260+ --data-urlencode " path=redaxo/src/addons/code/api_demo.csv" \
261+ " $BASE /api/code/file"
262+ ```
263+
264+ Update file content:
265+
266+ ``` bash
267+ curl -k -sS -X PUT \
268+ -H " Authorization: Bearer $TOKEN " \
269+ -H " Content-Type: application/json" \
270+ --data ' {"path":"redaxo/src/addons/code/api_demo.csv","content":"id;name\n1;Demo\n"}' \
271+ " $BASE /api/code/file"
272+ ```
273+
274+ Delete file:
275+
276+ ``` bash
277+ curl -k -sS -X DELETE -G \
278+ -H " Authorization: Bearer $TOKEN " \
279+ --data-urlencode " path=redaxo/src/addons/code/api_demo.csv" \
280+ " $BASE /api/code/file"
281+ ```
282+
283+ Required token scopes:
284+
285+ - ` code/capabilities `
286+ - ` code/files/list `
287+ - ` code/files/create `
288+ - ` code/file/read `
289+ - ` code/file/update `
290+ - ` code/file/delete `
291+
292+ #### Copilot instructions example
293+
294+ Copy this block into your project-level ` .github/copilot-instructions.md ` if you want consistent implementation rules for this addon API:
295+
296+ ``` md
297+ ## Code AddOn API Conventions
298+
299+ For ` redaxo/src/addons/code ` API routes registered into the ` api ` addon:
300+
301+ - Register route packages only when the ` api ` addon is available:
302+ - ` rex_addon::get('api')->isAvailable() `
303+ - ` class_exists(\FriendsOfRedaxo\Api\RouteCollection::class) `
304+ - Keep implementation in ` lib/Api/RoutePackage/Code.php ` and ` lib/Api/CodeFileService.php ` .
305+ - Use ` FriendsOfRedaxo\Api\RouteCollection::registerRoute(...) ` with ` BearerAuth ` and tag ` code ` .
306+ - Provide backend mirror routes via ` lib/Api/RoutePackage/Backend/Code.php ` .
307+ - Respect code addon config flag ` enable_file_browser ` and return ` 403 ` if disabled.
308+ - Accept only file types from ` FriendsOfRedaxo\Code\EditorConfig::getAllowedExtensions() ` .
309+ - Keep directory/path restrictions inside REDAXO base path and block traversal via realpath checks.
310+ - Do not allow deletion of protected files (e.g. ` .htaccess ` , ` index.php ` , ` composer.json ` , ` boot.php ` , ` install.php ` ).
311+ - Keep route scopes stable for token management:
312+ - ` code/capabilities `
313+ - ` code/files/list `
314+ - ` code/files/create `
315+ - ` code/file/read `
316+ - ` code/file/update `
317+ - ` code/file/delete `
318+ ```
319+
180320### Backup & Trash
1813211 . Navigate to ** Code → Backup & Trash**
1823222 . ** Backups tab** - restore previous file versions
0 commit comments