QuickApi Extra

A MODX Extra to simplify API creation for Ajax and Javascript libraries like Vue.js or React

Nov 29

Over time using MODX, I've played around with different methods of making API calls. This Extra is my attempt at creating an API that almost anyone can use, while keeping all the scripting in the platform in the form of Snippets.

Some Other Things I Tried

  1. Use a parent resource with an alias of "api".
    1. Then create child resources to represent the methods or specific endpoints.
    2. Next set content type to JSON
    3. Update the JSON content type to have no ending rather than ".json"
    4. Put a Snippet in the resource to handle the processing

The Problem

This works fairly well, but can result in a JSON response that's different with each project. You have to remember how you accessed the parameters, how you passed them in, etc. The other limitation with this method is you are not able to handle dynamic URL parameters similar to REST APIs.

  • Example To-Do List: /api/lists/13/complete
    • Return the completed items from the To-Do List with ID 13

This would not be possible with the resource method. You'd have to use something like the below and pass in query parameters.

  • /api/lists?id=13&complete=true

The QuickApi Extra allows you to use either method you prefer.

QuickApi Solution

The goals of this solution are listed below. I'm working on more detailed documentation to give examples on usage and features.

  1. Install into any project through Extras > Installer.
    1. Meaning it needs to be an Extra
  2. Produce consistent behavior across projects since the same methodology is used across all.
  3. Provide a documented way of adding API/REST-like functionality.
  4. Provide a centralized authorization method that can be changed on a per API basis.

Further Details

The full documenation is available here: QuickApi Documentation