meteor Integration of 3rd Party APIs

Basic HTTP Call

Conceptually, integrate 3rd party REST APIs can be as simple as adding the http package and making a call to the external endpoint.

meteor add http

Create A Package For Your API Wrapper

Basic HTTP calls don't provide code-reusability, however. And they can get confused with all the other features you're trying to implement. For those reasons, it's common to implement an API wrapper.

Foo = {
  identify: function(input){
    return Http.get('' + input);    
  record_action_on_item: function(firstInput, secondInput){
    return Http.put('' + firstInput + '&' + secondInput);    

Meteor supports Http.get(),, Http.put(), etc, so that's undoubtably the best way to call your REST API.

If the API is chatty and verbose, you may receive multiple packets; in which case you'll need to reassemble them. This is a big hassle. If you think the API is returning multiple packets, you're probably going to want to use the 'request' npm module on the server. You'll want to use a Npm.require('request').

Create an Atmosphere Package For Your API Wrapper

After creating an API wrapper, it's likely that you may want to create an Atmosphere package to redistribute it and share it between applications. The files of your package will probably look something like this.


In particular, your foo-api-wrapper/package.js file will want to look something like this:

  summary: "Atmosphere package that impliments the Foo API.",
  name: "myaccount:foo",
  version: '0.0.1'

Package.on_use(function (api) {
    api.addFiles('foo.api.wrapper.js', ["client","server"]);

And your foo-api-wrapper/foo.api.wrapper.js should contain the Foo API wrapper object.

Include the API Package in your Application

At this point, you're still building your package, so you'll need to add the package to your application:

meteor add myaccount:foo

And eventually publish it to Atmosphere:

meteor publish myaccount:foo

Using the API Wrapper Object in your App

Now that we have all those pieces put together, you should now be able to make calls like the following from within your app:

Foo.record_action_on_item('view', "HackerNews');

Obviously you'll want to adjust function names, arguments, urls, and the like, to create the proper syntax for the API.