Building A Simple REST Server with ExpressionEngine 2.0

Our clients love having the ability to control the content of their web sites. Now that we've been in the business of creating iOS and Android apps for a bit, we're also finding that our clients love having the ability to control the content of their apps. While the platforms are totally different, we've found that our mobile applications can benefit from integration with our favorite CMS in much the same way as our web sites.  Today I'll show you how we use ExpressionEngine to easily provide content to our iOS and Android projects through a simple REST server.

Thought Process

You might think that this is going to be difficult. It's not. Simply shift your thinking from creating templates that display well-written prose inside of a beautiful design, to creating templates that expose various bits of content in a machine-readable format. For this example we'll choose JSON; although, you could easily choose XML, or with a little more work, a tab-delimited format. A really neat side-effect to building your API in ExpressionEngine is that it can coincide---and share content with---your existing web site!

Channels & Custom Fields

Nothing changes here. Set up your channels and custom fields just as you normally would when building a web site. When building out a REST API with ExpressionEngine, I tend to think of my channels as database tables and my custom fields as columns in the table. So if you're wanting to create an API that spits out products, you might have a products channel and each custom field would be attributes of that product.

Templates

Remember the shift in thinking I mentioned earlier? Here's where it matters. Your templates won't be outputting HTML code; rather, you'll be outputting JSON code---and it's super simple to setup. Here's an example template from an iOS project I worked on that pulled in Vimeo videos. The client is able to add/remove/change/reorder the videos at any time and the iOS app will download the new/changed content as it become available.

So let's break this down:

- First, I used a handly HTTP header plugin to create the appropriate JSON headers.
- Second, I setup a normal Channel:Entries tag to pull the videos from the channel.
- If no results were found, send to the 404 page. You can use the no_results conditional to output anything you wish. You could even set a new HTTP header.
- Finally, we build the JSON object with the attributes needed in the application.

One thing to note, a trailing comma in a JSON array is not valid. The Channel:Entries' backspace parameter is perfect for this situation. It will remove 8 characters from the final output which will eliminate the trailing comma.

The Result

Once we hit that template in our web browser---or our iOS application, the JSON containing our Vimeo video content should be displayed as you would expect. Admittedly, this is a very simple example. APIs that are used in the real world will want a few other features for rate limting, authentication, caching, etc. Through the use of both ExpressionEngine's built-in features and a few custom plugins, you should be able to build a decent API whose content can be managed effortlessly by your clients or staff.

5 comments

Jim Arment Very cool. Thanks for sharing. Tue, Feb 7, 2012 3:05pm
Natetronn Jesse is there a reason you opted for HTTP Headers vs JSON which is also from @_rsan? Tue, Feb 7, 2012 10:26pm
Jesse Bunch @Natetronn, yes for two reasons. 1) I wanted to provide as much as an EE only approach as I could. 2) The JSON addon you mention doesn't support filtering and such. Using the Channel:Entries tag, you'd be able to take advantage of search, categories in the url, etc. Tue, Feb 7, 2012 10:32pm
Amit Hey Jesse,Wish you would expand in this post by showing us how you handle inserts updates and deletes. How do you write data? Wed, Feb 8, 2012 3:43am
Aaron What would you recommend as the best way to have an iOS app authenticate first before doing this kind of data retrieval? I have some Channels that are only viewable to logged in members, but I'm not sure how to handle Auth via JSON like this. Wed, May 30, 2012 2:36pm

Share your thoughts

Comments are closed for this entry.

IE 6

We're sorry...

The browser you're using is not supported, and we strongly suggest that you upgrade.

We recommend using Firefox or Chrome.