Instantly create a REST API for your Meteor methods and publications with simple:rest!

Hi everyone!

I’ve been working on making it ultra simple to expose a JSON HTTP API from your Meteor app. All you have to do is add the simple:rest package:

meteor add simple:rest

Now, you can access all of your publications and methods defined with Meteor.methods and Meteor.publish via HTTP! You can load your data and call methods with JQuery, Ruby, Python, Android, iOS, etc. This should make it much easier to work with your Meteor server from other apps, tools, and programs.

Check out the documentation for more info. This is completely stable, but it’s a preview release because I want to get more feedback from people about whether this is the right direction. Read about the design and discuss feedback in this Hackpad.

This package is all about an automatic API for methods and publications; if you are looking for CRUD-style REST APIs for your collections, I recommend nimble:restivus.

I hope you like it!
Sashko

12 Likes

Thanks, this looks pretty awesome. This seems great for when setting up and using websockets from another non-Meteor client isn’t possible/worth the effort.

2 Likes

Thanks for creating this! In trying to figure it out…

I created a file called server.js which has a method:

Meteor.methods({
  'addSite':function(text) {
    var incomingData = text;
    return "Data: {{incomingData}}";
  }
})

How do I access the text passed to the method addSite via the post which I’m currently sending via curl:

curl --data “firstName=Gerald&lastName=Bailey” http://localhost:3000/methods/addSite/

Nevermind, I figured out my bug… hehe

1 Like

I’m having trouble with returning data from an async Meteor method.
How do we wait for an async Meteor method?

For example, this api call:

useEffect(() => {
    if (urlParams.has('bookingsUrl')) {
      axios.post('http://localhost:3000/methods/getBusinessDetails', [BUSINESS_NAME])
      .then(function (response) {
        const result = response.data;
        setLoading(false);
      })
      .catch(function (error) {
        console.log(error);
      });
    } else {
      setLoading(false);
    }
  }, []);

It works perfectly if the getBusinessDetails is a normal method

Meteor.methods({
  getBusinessDetails(businessName) {
     return { found: true }
  }
})

But if the getBusinessDetails is an async method, then the api call only returns {}

Meteor.methods({
  async getBusinessDetails(businessName) {
      const subscription = await stripe.subscriptions.list({
        customer: ownerAccount.stripeCustomerId
      });
     return { subscription: subscription }
  }
})

Try to use Promise.await:

import { Promise } from 'meteor/promise';

Meteor.methods({
  getBusinessDetails(businessName) {
    if(Meteor.isServer){
      const subscription = Promise.await(stripe.subscriptions.list({
        customer: ownerAccount.stripeCustomerId
      }));
      return { subscription: subscription }
    }
  }
})

1 Like

That works nicely. Thanks.