How to create an API using meteor?

I need to build an API. e.g. calling localhost:3000/api/param should give me the parameter param and should output an JSON string. I don’t know how to get this done. So I thought to use a route (I’m using flowrouter), so I created something like this:

FlowRouter.route('/api/:type', {
	action: function(params, queryParams) {
		console.log(params, queryParams);

This gives my the object {type: 'param'} in the console.

But I need to output a JSON string instead an output in the console. Also there is also rendered partial HTML-content of the application, but in this case I need to get a JSON string as output.

How do I do that? Maybe this is a complete wrong way…

If you’re starting from scratch and/or going to be doing a lot with the API, it may be helpful to have a look at Apollo, which is considered by many to have great advantages vs. REST.

What you want is meteorhacks:picker. Don’t forget the add-on for JSON parsing.


Thank you. Now I am using Picker and it is working very easily. The only thing is, that I need to authentificate a user. So how do I pass login data and check this on server side in a meteor way?


Picker.route( '/api/:username/:password', function( params, request, response ) {
    const username = params.username;
    const password = params.password;
    // authentificate ???

If you need user authentication, use restivus, use it myself to provide simple REST API delivering json and happy with it.

restivus is not working for me as I have to filter a collection. Can’t use getAll or a single document, but need to get some documents from the collection…

Why don’t you call a Meteor.method to filter the needed collection ?

Needs a refresh but the basics concepts here may help:

1 Like

@ffxsam which add-on? What is the URL?

The body-parser npm package. Example:

import { Picker } from 'meteor/meteorhacks:picker';
import bodyParser from 'body-parser';

Picker.middleware(bodyParser.urlencoded({ extended: false }));

// Now you're ready to use Picker as documented, and it will handle JSON data

Look into the simple:rest package for authentication, it allows you to receive a meteor token on login (which you can save in local storage) and then the request can authenticate the user by looking up the user by their token. simple:rest also has a lot of nice things to replace picker… it’s way more polished IMHO.

1 Like

Diden’t have much luck with Picker when I tried to parse a form previously. Take a look at works much better for me and not outdated as Picker.

1 Like

Cool - always looking for better ways to do things! I’ll check out simple:rest and webapp.

1 Like

You can create custom endpoint that can hold any logic.

Picker is working in development but not not in production getting null data in production

const app = express()
WebApp.connectHandlers.use('/api', Meteor.bindEnvironment(app))