What is the proper way to set up API endpoints for usage with Keystone?
Asked Answered
A

2

5

It's not clear in the docs how one would use existing Keystone models to expose API endpoints that return json within a Keystone.js app. I would simply like to be able expose REST API endpoints with Keystone and be able to use the Keystone CMS capabilities to manage content via interacting with those endpoints. Thanks!

Angelitaangell answered 20/3, 2015 at 19:54 Comment(3)
I'm not quite sure what you mean by "set up". Keystone currently has an internal API which is consumed by the Admin UI. You won't find anything in the docs because, like I said, it's an internal API, not intended for public consumption (that's not to say you can't use it anyway). You can always create your own, which I suspect you already knew. So, when you say "set up" do you mean "configure existing" or do you mean "create new"?Premonitory
I'd like to be able to use the Keystone models to build a CRUD API that works with Keystone's CMS. When you say internal API, does that mean you're not supporting this method of using a REST API?Angelitaangell
When I say "internal" API I mean an API that is not "public" (i.e. not intended for public consumption), which is why it isn't documented. It is used by the Keystone's Admin UI. That doesn't mean you cannot use it. You're of course free to use it, but bare in mind that since it is not a public API it may change without notice. There are open issue on the subject (see Keystone GH issue #396), and one of the contributors (@creynders) who created a module called restful-keystone.Premonitory
T
5

Now that they've standardized the admin API I found that it's pretty trivial to use the same methods. For my read only APIs that are powering my react app I've done put something like this in my routes/index.js

router.get('/api/:list/:format(export.csv|export.json)',middleware.initList,require('keystone/admin/server/api/list/download'));

And I've made my own version of the admin initList middleware:

exports.initList = function(req, res, next) {
  console.log('req.keystone', req.keystone);
  req.keystone = keystone;
  req.list = keystone.list(req.params.list);
  if (!req.list) {
    if (req.headers.accept === 'application/json') {
      return res.status(404).json({ error: 'invalid list path' });
    }
    req.flash('error', 'List ' + req.params.list + ' could not be found.');
  }
  next();
};
Teagan answered 26/5, 2017 at 20:46 Comment(0)
P
3

You may consider using:

I've never actually used either of these because I have my own implementation, which I will open source once Keystone implements it plugin architecture (see Keystone Issue #912: Proposed Keystone Package Architecture).

I suspect many other similar modules will start surfacing once Keystone is more "plugin friendly".

Premonitory answered 24/3, 2015 at 9:3 Comment(1)
Any update on this. I am also looking for similar optionsDeterminism

© 2022 - 2024 — McMap. All rights reserved.