Skip to content
Go to GoCardless homepage
Pricing
LoginSign up
BreadcrumbResourcesGoCardless

gc-http-factory: an easier way to work with APIs in Angular

By Jack FranklinSep 20142 min read

Today we're announcing and open sourcing gc-http-factory, a tool for working with APIs in Angular.

Using APIs in Angular without HttpFactory

In a regular Angular app you'd use a service to make requests to an API:

angular.app('app', []).factory('UsersService', function($http) {
  function findOne(id) {
    return $http.get('/api/users/' + id);
  };

  function findAll() {
    return $http.get('/api/users');
  };

  function create() {
    return $http.post('/api/users');
  };

  return {
    findOne: findOne,
    findAll: findAll,
    create: create
  };
});

If you've got lots of API resources which follow similar conventions the above becomes repetitive pretty quickly.

Using HttpFactory

HttpFactory provides an abstraction for creating services that make requests to API resources. You use HttpFactory to create your service and methods by telling it some information about your API.

For example, to create the UsersService we defined previously, you'd use:

angular.app('app', ['gc.httpFactory']).factory('UsersService', function(HttpFactory) {
  return HttpFactory.create({
    url: '/api/users/:id'
  }, {
    findOne: { method: 'GET' },
    findAll: { method: 'GET' },
    create: { method: 'POST' }
  });
});

You can then use the service you've created as normal:

UsersService.findAll(); //=> GET /api/users

UsersService.findOne({
  params: { id: 2 }
}); //=> GET /api/users/2

UsersService.create({
  data: { name: 'Jack' }
}); //=> POST /api/users with { name: 'Jack' } as data

Installation

You can install gc-http-factory from Bower:

bower install gc-http-factory

Or just grab the source from GitHub.

Contributing

We've been using HttpFactory in our Angular apps for a while and we're really happy with how cleanly it lets us create components to interface with our APIs. It's avoided duplication across our Angular apps and we hope it might do the same for you.

If you have any suggestions, ideas or bugs, please either report them on GitHub or feel free to find me on Twitter.

We're hiring developers

See job listing

GoCardless makes it easy to collect recurring payments

Sign upContact sales

Interested in automating the way you get paid? GoCardless can help

Contact sales

Contact Us

Sales

Contact sales

+44 20 8338 9539

Support

Request support

+44 20 8338 9540

Seen 'GoCardless Ltd' on your bank statement? Learn more

GoCardless Ltd., Sutton Yard, 65 Goswell Road, London, EC1V 7EN, United Kingdom

GoCardless (company registration number 07495895) is authorised by the Financial Conduct Authority under the Payment Services Regulations 2017, registration number 597190, for the provision of payment services. GoCardless SAS (23-25 Avenue Mac-Mahon, Paris, 75017, France), an affiliate of GoCardless Ltd (company registration number 834 422 180, R.C.S. PARIS), is authorised by the ACPR (French Prudential Supervision and Resolution Authority), Bank Code (CIB) 17118, for the provision of payment services.