Wireframe API

Wireframe API is an optional add-on module that provides a JSON API for accessing Wireframe's features. Typical use cases include JavaScript based views and third party consumers (services) requesting data from your site, but you could also use it to set up a simple headless CMS.

Wireframe API is a separate add-on module bundled with the main Wireframe package. By default this module (which is available as of Wireframe 0.12.0) is uninstalled and none of the built-in API endpoints are enabled, so getting started is a three-part process:

  1. Install the Wireframe API module
  2. Enable one or more endpoints (via module config or via the $config->wireframeAPI array — see "Configuration settings" for more details)
  3. Call the API module's init() method somewhere in your own code and render the resulting response

For the third step you would usually want to create a new template that has URL segments enabled and is routed through the Wireframe bootstrap file (alternate template file setting). Insert the code snippet below into the applicable controller file (in this example this would be /site/templates/controllers/APIController.php):

<?php

namespace Wireframe\Controller;

/**
 * Controller class for the API template
 */
class APIController extends \Wireframe\Controller {

    /**
     * Render method
     */
    public function render() {
        echo $this->wire('modules')->get('WireframeAPI')->init()->sendHeaders()->render();
        $this->view->setLayout(null)->halt();
    }

}

(You could also create a template that isn't routed through the bootstrap file. This will work just fine in most cases, but obviously you won't have access to any of the site-wide settings etc. defined in the bootstrap file, so pick whichever approach makes most sense to you.)

Finally, create a page using this template. It doesn't really matter what you call this page, or where you place it, but you may want to avoid something as generic as "api" as that could potentially get the attention of bots (or it may clash with some other module later on).

Requests for this page are now served by the API module. Now, in order to get something useful out of the API, the requested URL has to match an enabled API endpoint. Here's an example request for the "components" endpoint; here "wireframe-api" is the name of the page you created for the API, and Card is the name of one of your components:

curl https://www.yoursite.tld/wireframe-api/components/Card/
{
    "success": true,
    "message": "",
    "path": "wireframe-api\/components\/Card",
    "args": [],
    "data": {
        "json": {
            "title": "Wireframe API",
            "text": "Provides a JSON API for accessing Wireframe's features",
        },
        "rendered": "<div class=\"card\"><h2>Wireframe API<\/h2><p>Provides a JSON API for accessing Wireframe's features<\/p><\/div>"
    }
}

It's also possible to define the API path programmatically while initializing the API object, and/or pass an array of arguments for the endpoint:

$api->init('components/Card', [
    'argument' => 'value',
]);
Back to top