Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Runway to create dynamic REST API endpoints


Can I use Runway to create dynamic REST API endpoints


Yes, you can use Runway to create dynamic REST API endpoints in Statamic. Here's how it works:

1. Enable the REST API in Statamic:
- First, make sure the REST API is enabled in your Statamic project. You can do this by adding the following to your `.env` file:


     STATAMIC_API_ENABLED=true
     

- Alternatively, you can enable it for all environments in the `config/statamic/api.php` file:
php
     'enabled' => true,
     

2. Enable Runway Resources in the API Configuration:
- In the `config/statamic/api.php` file, add a `'runway'` key to the `'resources'` section and list the Eloquent models you want to expose through the REST API.
- For example:

php
     'resources' => [
         'collections' => true,
         'taxonomies' => true,
         'runway' => [
             'products' => true,
             'orders' => true,
         ],
     ],
     

- This will create two endpoints for each of your Runway resources:
- `/api/runway/{resourceHandle}` to retrieve a list of models
- `/api/runway/{resourceHandle}/{id}` to retrieve a specific model

3. Configure Filtering and Sorting:
- You can enable filtering for your Runway resources by defining a list of `'allowed_filters'` in the `config/statamic/api.php` file.
- For example:

php
     'runway' => [
         'products' => [
             'allowed_filters' => ['name', 'slug', 'price'],
         ],
     ],
     

- This will allow you to filter the `/api/runway/products` endpoint by `name`, `slug`, and `price` fields.

4. Customize the API Endpoints:
- If needed, you can further customize the API endpoints by modifying the `'route'` configuration in `config/statamic/api.php`.
- For example, you can change the base URL from `/api` to something else:

php
     'route' => 'not-api',
     

By following these steps, you can use Runway to create dynamic REST API endpoints in your Statamic project, allowing you to expose your Eloquent models through a RESTful API. This can be particularly useful when building integrations with other systems or creating custom front-end applications that consume your Statamic-powered data.

Citations:
[1] https://statamic.dev/graphql
[2] https://runway.duncanmcclean.com/rest-api
[3] https://runway.duncanmcclean.com/graphql
[4] https://statamic.dev/rest-api
[5] https://accreditly.io/articles/how-to-use-graphql-in-statamic