Amygdala: A JavaScript REST Client

2014-08-14 00:00:00 +0100 by Alex R. Young

Over the last few years I've worked almost exclusively by designing server-side APIs totally separately to the UI. Client-side code is just another client, structured with something like Backbone or Angular. This works well when you develop applications that have multiple front-ends (public site, admin area), or you've got desktop and mobile clients.

Marco Louro sent in Amygdala (GitHub: lincolnloop / amygdala, License: BSD, npm: amygdala), a JavaScript REST library designed specifically for this type of work. It's inspired by Hoodie, but optimised for custom API backends instead of a specific API or database.

Amygdala was born out of the desire to reduce the complexity involved with managing multiple JavaScript modules (controllers or models/collections) that do essentially the same thing, fetch and sync data.

It works by using a constructor function that accepts configuration and resource paths. Data is cached to localStorage, and updates to data after subsequent modifications results in events that you can subscribe to:

store.on('discussions:change', function() {
  // re-render our view

When you set up the RESTful API, you can specify things like HTTP headers and foreign key relationships:

var store = new Amygdala({
  'config': {
    'apiUrl': 'http://localhost:8000',
    'idAttribute': 'url',
    'headers': {
      'X-CSRFToken': getCookie('csrftoken')
    'localStorage': true
  'schema': {
    'users': {
      'url': '/api/v2/user/'
    'teams': {
      'url': '/api/v2/team/',
      'orderBy': 'name',
      'oneToMany': {
        'members': 'members'
    'members': {
      'foreignKey': {
        'user': 'users'

When I use a library that focuses on data binding, like Knockout, server communication always feels hard to get right. Amygdala looks like a great library to pair with Knockout -- it'll help make server calls cleaner and also add locally cached data without much extra effort.