Create a JSON API for Your Content with Element API

Have you ever needed to output your entries in pure JSON? While not impossible, it's never been all that easy in Craft. Those days are behind us now though, thanks to our new Element API plugin.

Element API is a simple plugin that allows you to define “endpoints" that together make up your API. Each endpoint consists of a URL pattern and some settings that determine what data should be returned when the URL pattern is matched.

You can set up endpoints that return entries, categories, tags, assets, users, global sets, and even Matrix blocks. If you have a plugin installed that provides its own custom element types (e.g. Sprout Forms), you can create endpoints for those, too.

Endpoints can return data about a single element, or multiple. If you're outputting multiple elements, you can even have the results automatically paginated for you.

The actual data returned for each element is completely up to you. You can include custom field values, information about related elements, and anything else you can think of. Take a look at the documented examples to get an idea of what's possible.

So why did we do this as a plugin, as opposed to built-in functionality? The main reason is that Element API is powered by Fractal under the hood, and Fractal requires PHP 5.4, while Craft 2 still requires PHP 5.3. We expect we'll be adding similar functionality directly into the core for Craft 3, which will require a newer version of PHP, but releasing it as a plugin allowed us to get this feature into your hands today.

We hope you find Element API useful. We think it's going to make building React/Angular/mobile apps that fetch content from Craft a lot easier. If you do anything interesting with it, please let us know in the comments!

comments powered by Disqus