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
- Use a parent resource with an alias of "api".
- Then create child resources to represent the methods or specific endpoints.
- Next set content type to JSON
- Update the JSON content type to have no ending rather than ".json"
- Put a Snippet in the resource to handle the processing
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:
- 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.
The QuickApi Extra allows you to use either method you prefer.
The goals of this solution are listed below. I'm working on more detailed documentation to give examples on usage and features.
- Install into any project through Extras > Installer.
- Meaning it needs to be an Extra
- Produce consistent behavior across projects since the same methodology is used across all.
- Provide a documented way of adding API/REST-like functionality.
- Provide a centralized authorization method that can be changed on a per API basis.
The full documenation is available here: QuickApi Documentation