Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

keystonejs

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!

like image 556
cameronroe Avatar asked Mar 20 '15 19:03

cameronroe


People also ask

How does API endpoints work?

APIs work by sending requests for information from a web application or web server and receiving a response. In other words, API endpoints are the specific digital location where requests for information are sent by one program to retrieve the digital resource that exists there.

How do you define API endpoints?

When an API interacts with another system, the touchpoints of this communication are considered endpoints. For APIs, an endpoint can include a URL of a server or service. Each endpoint is the location from which APIs can access the resources they need to carry out their function.

What are API endpoints example?

An API Endpoint is the URL for a server or a service. These APIs operate through responses and requests — that is you make a request and the API Endpoint makes a response. A simple example of this is this particular Websites and article. The Websites is Medium, and your Web Browser makes a request for the content.

How do I access API endpoints?

Through the dataset URL: You can get the API endpoint by simply taking the dataset's UID and replacing it in this string: https://domain/resource/UID.extension *where the extension is the data format you's like to pull the data as. For a full list of extension formats please go here.


1 Answers

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();
};
like image 108
thewolff Avatar answered Jan 01 '23 00:01

thewolff