Moodle Plugins directory: Api Query | Moodle.org
Api Query
Local plugins ::: local_apiquery
Maintained by
Cesar Machado
Api Query — A Moodle local plugin that lets administrators configure custom SQL queries through the admin UI and expose them as REST API endpoints. Each query supports typed parameters, optional defaults, and built-in SQL validation. Designed to reduce the load on Moodle's native web services by replacing multiple individual API calls with a single optimized query per sync cycle.
Latest release:
3 sites
52 downloads
1 fans
Current versions available: 1
Turn any SQL query into a REST endpoint — without writing code.
local_apiquery is a Moodle local plugin that allows administrators to define SQL queries through a visual interface and instantly expose them as authenticated REST API endpoints.
It includes typed parameters, security validation, and execution logging, making it ideal for integrations with external systems such as BI tools, mobile apps, dashboards, or custom scripts.
🚀 Why use this plugin?
Moodle's web services layer is powerful, but typically requires PHP development for every new endpoint.
local_apiquery removes that friction:
- Define a SQL query from the admin panel
- Configure its parameters
- Use it instantly as a REST API endpoint
✨ Key Features
| Feature | Description |
|---|---|
| Visual Query Editor | Create, edit, and enable/disable queries directly from the admin panel — no deployments required |
| Typed Parameters | Define type (int, float, bool, text), required/optional, and default values |
| SQL Security Validator | Allows only SELECT queries and blocks dangerous operations |
| Interactive Tester | Test queries with real parameters before publishing |
| Execution Logs | Logs execution time, row count, and errors for every API call |
| Export / Import | Move queries between environments using JSON files |
| Discovery Endpoint | Use list_queries to discover available endpoints and their schemas |
📋 Requirements
- Moodle 4.1 or later
- Web services enabled
- REST protocol enabled
⚙️ Installation
Option A — Via Moodle UI
- Download the plugin ZIP
- Go to
Site administration > Plugins > Install plugins - Upload the ZIP and follow the instructions
Option B — Manual
- Copy the plugin into
/local/apiquery/ - Visit site administration to trigger installation
After installation
- Enable the Api Custom Queries service
- Create a web service token
- Assign capability
local/apiquery:execute
⚡ Quick Start
1. Create a query
Example:
| Shortname | active_users_since |
| SQL | SELECT id, username, email FROM {user} WHERE timeaccess > :since AND deleted = 0 |
| Parameter | since (int, required) |
2. Call the API
POST /webservice/rest/server.php wsfunction=local_apiquery_execute_query wstoken=YOUR_TOKEN moodlewsrestformat=json shortname=active_users_since params[0][name]=since params[0][value]=1738000000
3. Response
{
"success": true,
"rows_count": 3,
"execution_ms": 12,
"rows": [...]
}
Note: Rows are returned as {key, value} pairs due to Moodle web service schema limitations.
🔐 Security
- Only
SELECTqueries are allowed - Access to sensitive tables is blocked
- Only named placeholders (
:param) are allowed - Multiple statements are blocked
- Access controlled via Moodle capabilities
📦 Export & Import
- Export queries to JSON
- Import with conflict validation
- Includes environment metadata
📄 License
GNU GPL v3 or later
http://www.gnu.org/copyleft/gpl.html
Useful links
Contributors
Cesar Machado (Lead maintainer)
Please login to view contributors details and/or to contact them
Comments