### Example GET requests for standard routing Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Requests/Requests/30_Variables.html Example GET requests that are routed to the Example->getNewsAction() method when using standard routing. ```text https://www.mysite.com/api/example/news/ https://www.mysite.com/api/example/news/1 https://www.mysite.com/api/example/news/article https://www.mysite.com/api/example/news/article/1 https://www.mysite.com/api/example/news/article/1/2/3 ``` -------------------------------- ### VanillaJS starter template HTML Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Examples/Examples/LegacyJS/10_simple.rst.txt A complete HTML starter template for a VanillaJS demo that connects to the nnrestapi. Includes Bootstrap CSS from a CDN. Copy and paste to get started. ```html nnrestapi VanillaJS Demo ``` -------------------------------- ### Full UploadPathHelper class example Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Annotations/Annotations/60_Upload.rst.txt Full example of a custom upload path helper that returns a date-based folder path. ```php '1:/api/' . date('Y-m') . '/' ]; } } ``` -------------------------------- ### Get request settings with $this->request->getSettings() Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Requests/Requests/30_Variables.html Returns the settings for the current request, including the instructions for processing the data defined in the TypoScript setup. ```text [ ... 'fileUploads' => [ 'default' => [ 'defaultStoragePath' => '1:/api/', 'file' => '1:/api/tests/', ] ], 'globalDistillers' => [ 'TYPO3\CMS\Extbase\Domain\Model\Category' => [ 'exclude' => 'parent' ] ] ... ] ``` -------------------------------- ### GET request URL for the custom checkAccess() example Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Access/Access/Annotation.html The URL to reach the example endpoint with a GET request. The endpoint is accessible only if the custom checkAccess() method returns TRUE. ```text https://www.mysite.com/api/test/example ``` -------------------------------- ### Set string example data with @Api\Example Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Annotations/Annotations/30_Example.rst.txt Adds a simple string as example data for the request body in the backend module's test bed. ```php @Api\Example("this is an example") ``` -------------------------------- ### Set JSON example data with @Api\Example Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Annotations/Annotations/30_Example.rst.txt Adds a JSON-formatted string as example data for composing a JSON request in the test bed. ```php @Api\Example("{'username':'david', 'password':'mypassword'}") ``` -------------------------------- ### Example URLs for default routing Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Requests/Requests/10_Standard.html Two example URLs showing the default routing pattern: one with a string method name and one with an integer method name. ```text https://www.mywebsite.com/api/article/all https://www.mywebsite.com/api/article/1 ``` -------------------------------- ### HTTP Basic Auth request examples for TYPO3 REST API (PHP, axios, jQuery, pure JS) Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Authentication/Authentication/10_http_auth.rst.txt These examples show how to send HTTP Basic Auth requests to the TYPO3 REST API using PHP, axios, jQuery, and pure JavaScript. Each snippet sets the Authorization header with base64-encoded credentials and handles the response. The PHP example uses file_get_contents with credentials in the URL; the axios example uses the auth option; the jQuery example sets the header manually; the pure JS example uses fetch with a manually constructed Authorization header. ```php { console.log( data ); }).catch( ({response}) => { console.log( response.data ); }); ``` ```javascript var method = 'GET'; var url = 'https://www.mywebsite.com/api/endpoint'; var username = 'john'; var password = 'xxxx'; var payload = JSON.stringify({ title: 'Test' }); $.ajax({ url: url, type: method, data: payload, crossDomain: true, beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ':' + password)); }, }).done(function (result) { $('#result').text( JSON.stringify(result) ); }).fail(function (error) { alert( 'Error ' + error.status + ': ' + error.responseJSON.error ); $('#result').text( 'ERROR: ' + JSON.stringify(error) ); }); ``` ```javascript const url = 'https://www.mywebsite.com/api/your/endpoint'; const auth = { username: 'john', password: 'xxxx' }; const xhrConfig = { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa(`${auth.username}:${auth.password}`) }, }; fetch( url, xhrConfig ) .then( async response => { // convert the result to a JavaScript-object let data = await response.json() if ( !response.ok ) { // reponse was not 200 alert( `Error ${response.status}: ${data.error}` ); } else { // everything ok! console.log( data ); } }); ``` -------------------------------- ### Example URL for GET request with uid Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Examples/Examples/10_creating_an_api.rst.txt Shows the URL format for accessing the Article endpoint with a uid. The uid is passed as the last part of the URL and is automatically mapped to the $uid argument. ```php https://www.mysite.com/api/article/1 ``` -------------------------------- ### Frontend API request examples in PHP, JavaScript (fetch, XHR, Axios, jQuery) Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Localization/Index.html These examples show how to call a REST API endpoint with different HTTP clients: PHP cURL, JavaScript fetch, XMLHttpRequest, Axios, and jQuery. Each sets the Accept-Language header to request localized content. The PHP example includes an optional SSL verification disable for certificate issues; the fetch example handles non-OK responses by alerting the error; the XHR example alerts on HTTP errors and network errors; Axios and jQuery handle errors via their promise/callback mechanisms. ```php {$dump}"; ``` ```javascript const url = 'https://www.mysite.com/api/endpoint'; const language = 'de-DE'; const xhrConfig = { method: 'GET', headers: { 'Content-Type': 'application/json', 'Accept-Language': language }, }; fetch( url, xhrConfig ) .then( async response => { let data = await response.json() if ( !response.ok ) { alert( `Error ${response.status}: ${data.error}` ); } else { console.log( data ); document.getElementById('result').innerText = data.html; } }); ``` ```javascript var url = 'https://www.mysite.com/api/endpoint'; var language = 'en-EN'; var xhr = new XMLHttpRequest(); xhr.overrideMimeType('application/json'); xhr.open('get', url); xhr.setRequestHeader('Accept-Language', language); xhr.onload = function () { var data = JSON.parse( xhr.responseText ); if (xhr.status != 200) { alert('Error!'); } console.log( data ); }; xhr.onerror = function () { alert('Some other error... probably wrong url?'); }; xhr.send(); ``` ```javascript const url = 'https://www.mysite.com/api/endpoint'; const language = 'en-EN'; const headers = { 'Accept-Language': language }; axios({ method: 'get', url: url, headers: headers }).then( ({data}) => { console.log( data ); }).catch( ({response}) => { console.log( response.data ); }); ``` ```javascript const url = 'https://www.mysite.com/api/endpoint'; const language = 'en-EN'; const headers = { 'Accept-Language': language }; $.ajax({ url: url, type: 'GET', headers: headers }).done((data) => { console.log( data ); }).fail((error) => { alert( `Error ${error.status}: ${error.responseJSON.error}` ); }); ``` -------------------------------- ### Example URLs routed to getNewsAction Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Requests/Requests/30_Variables.rst.txt Example URLs that are routed to the Example->getNewsAction() method when using standard routing. The method name determines the endpoint. ```php https://www.mysite.com/api/example/news/ https://www.mysite.com/api/example/news/1 https://www.mysite.com/api/example/news/article https://www.mysite.com/api/example/news/article/1 https://www.mysite.com/api/example/news/article/1/2/3 ``` -------------------------------- ### Accept-Language header examples Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Localization/Localization/10_backgrounds.html Shows three example Accept-Language headers for German, French, and Italian, each sent with the same URI to request localized content. ```http // Ick sprecke Deutsch! Accept-Language: de-DE // Je parle Baguette Accept-Language: fr-FR // Il pablo Parmegano Accept-Language: it-IT ``` -------------------------------- ### Accept-Language header examples Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Localization/Localization/10_backgrounds.rst.txt Shows example Accept-Language headers for German, French, and Italian. These headers are sent with the request to indicate the desired language. ```php // Ick sprecke Deutsch! Accept-Language: de-DE // Je parle Baguette Accept-Language: fr-FR // Il pablo Parmegano Accept-Language: it-IT ``` -------------------------------- ### Full example of an API endpoint with @Api\Example Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Annotations/Annotations/30_Example.html Complete class example showing the @Api\Example annotation used in an endpoint method. The class extends AbstractApi and uses the @Api\Endpoint and @Api\Access annotations. ```php 'result']; } } ``` -------------------------------- ### Example response JSON from nnrestapi file upload Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Examples/Examples/Axios/30_fileuploads.html Example response from the nnrestapi after processing the multipart upload. The 'UPLOAD:/myfile' placeholder is replaced with the file path 'fileadmin/api/image.jpg'. ```json { "title": "This is the title", "text": "This is the bodytext", "image": "fileadmin/api/image.jpg" } ``` -------------------------------- ### Full example of @Api\Access usage Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Access/Index.html Full example of a TYPO3 REST API endpoint class using the @Api\Access annotation. Requires the use statement 'use Nng\Nnrestapi\Annotations as Api;' at the top of the script. ```php 'welcome!']; } } ``` -------------------------------- ### Define multiple access groups in site YAML or TypoScript Setup Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Access/Index.html Defines multiple access groups with different user types and combinations. The identifier names are arbitrary; you can choose any names that make sense for your setup. ```yaml nnrestapi: accessGroups: limitedUser: fe_users[david,marc] adminUsers: fe_users[1,2,3], be_users, be_admins viewOnlyUsers: fe_groups[apiViewers] ``` ```typoscript plugin.tx_nnrestapi.settings { accessGroups { limitedUser = fe_users[david,marc] adminUsers = fe_users[1,2,3], be_users, be_admins viewOnlyUsers = fe_groups[apiViewers] } } ``` -------------------------------- ### basic.apiKeys configuration example Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Configuration/Configuration/30_extmanager.html Configure global API users in the Extension Manager. Each line contains a username and API key separated by a colon. The default user 'examplefeUserName' is excluded from access. ```text user1:theApiKeyOfUser1 user2:theApiKeyOfUser2 ``` -------------------------------- ### Get list of all countries using nnhelpers Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Responses/Responses/10_Simple.html Example using EXT:nnhelpers to fetch all static countries from the database and return them as an array. ```php /** * Get list of all static countries from the database * * @Api\Access("public") * @return array */ public function getCountriesAction() { $countries = \nn\t3::Environment()->getCountries(); return $countries; } ``` -------------------------------- ### @Api\Access() annotation examples Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Authentication/Authentication/10_http_auth.html Examples of @Api\Access() annotations to control endpoint access. Use "api_users" for all API users, "api_users[john]" for a specific user, or "config[myUsers]" for users defined in TypoScript setup. ```php // Expose this endpoint to ALL api_users @Api\Access("api_users") // ... or only to the user "john" @Api\Access("api_users[john]") // ... or to the users defined in the TypoScript setup @Api\Access("config[myUsers]") ``` -------------------------------- ### basic.apiKeys example format Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Configuration/Configuration/30_extmanager.rst.txt Shows the expected format for the basic.apiKeys setting: one user per line, with username and API key separated by a colon. The default value is 'examplefeUserName:exampleApiKey'. ```text user1:theApiKeyOfUser1 user2:theApiKeyOfUser2 ``` -------------------------------- ### Change default header value in TypoScript Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Responses/Responses/30_Headers.rst.txt Set a new value for an existing header key in the TypoScript setup. This example changes Access-Control-Allow-Credentials to false. ```typoscript plugin.tx_nnrestapi.settings.response { headers { Access-Control-Allow-Credentials = false } } ``` -------------------------------- ### Configure global HTTP Basic Auth API keys (basic.apiKeys) Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Authentication/Authentication/10_http_auth.rst.txt Enter global HTTP Basic Auth credentials in the extension configuration field "API-Keys for BasicAuth" (basic.apiKeys). Each line contains a username and password separated by a colon. ```bash username_1:password_1 username_2:password_2 username_3:password_3 ... ``` -------------------------------- ### @Api\Access() annotation examples for HTTP Basic Auth Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Authentication/Authentication/10_http_auth.html Examples of @Api\Access() annotations to expose endpoints to all fe_users with an apiKey, to a specific fe_user 'john', or to users defined in TypoScript setup. These annotations are used to set the correct rights for endpoints accessible via HTTP Basic Auth. ```php // Expose this endpoint to ALL fe_users that have an apiKey @Api\Access("api_users") // ... or only to the fe_user "john" @Api\Access("api_users[john]") // ... or to the users defined in the TypoScript setup @Api\Access("config[myUsers]") ``` -------------------------------- ### VanillaJS starter template for nnrestapi demo Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Examples/Examples/PlainJS/10_simple.html Full copy-and-paste starter template for a VanillaJS demo of the nnrestapi. It builds a Bootstrap form that lets you pick a request method (GET, POST, PUT, PATCH), enter an endpoint URL, a title, and a body text, then sends the request with fetch and displays the JSON response in a
 element. The script sets the Content-Type header to application/json and only includes a JSON body for methods other than GET and DELETE; on a non-OK response it alerts with the status and the error field from the response JSON.

```html


    
        
        
        nnrestapi VanillaJS Demo

        

        
    
    
        
Result
``` -------------------------------- ### GET request URL for article uid Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Examples/Examples/10_creating_an_api.html Example URL for retrieving an article by uid. The integer at the end is automatically mapped to the $uid argument and triggers the getIndexAction(). ```text https://www.mysite.com/api/article/1 ``` -------------------------------- ### Full axios starter template with login form and testbed for TYPO3 REST API Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Examples/Examples/Axios/20_auth.html Full starter template for an axios-based login form and testbed against a TYPO3 REST API. Includes a login form that authenticates a frontend user and stores the returned token as a default Authorization header, then shows a test form for sending GET, POST, PUT, PATCH, and DELETE requests. Requires the axios library and a valid auth endpoint URL; errors are shown via alert with the response status and error message. ```html nnrestapi axios Demo with authentication

    
``` -------------------------------- ### Send GET request with Accept-Language header using axios and jQuery Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/_sources/Localization/Index.rst.txt Two JavaScript examples showing how to send a GET request with an 'Accept-Language' header to a REST API endpoint. The first uses axios, the second uses jQuery's $.ajax. Both set the language to 'en-EN' and handle success and error responses. ```javascript const url = 'https://www.mysite.com/api/endpoint'; const language = 'en-EN'; const headers = { 'Accept-Language': language }; axios({ method: 'get', url: url, headers: headers }).then( ({data}) => { console.log( data ); }).catch( ({response}) => { console.log( response.data ); }); ``` ```javascript const url = 'https://www.mysite.com/api/endpoint'; const language = 'en-EN'; const headers = { 'Accept-Language': language }; $.ajax({ url: url, type: 'GET', headers: headers }).done((data) => { console.log( data ); }).fail((error) => { alert( `Error ${error.status}: ${error.responseJSON.error}` ); }); ``` -------------------------------- ### VanillaJS starter template with login form Source: https://docs.typo3.org/p/nng/nnrestapi/main/en-us/Examples/Examples/LegacyJS/20_auth.html Starter template with a login form and a test form for sending JSON requests (GET, POST, PUT, PATCH, DELETE) to a TYPO3 REST API. It includes Bootstrap 5 styling and a helper function for older browsers that do not support `fetch()`. The code is truncated at the end of the provided source. ```html nnrestapi axios Demo with pure JavaScript for older browser