The npm package moodle-core-cli is a Node.js library and command for Moodle's own web services. It offers friendly operations such as create_course and handles Moodle function names, parameters, and version differences for you. Use it when your site cannot install the MoodlIA plugin.
What it does
- Friendly operations for courses, users, enrolments, groups, grades, and more, on Moodle 4.5 to 5.3.
- Read-only by default: writes need
--allow-writeand deletions also --yes. - JSON output and stable exit codes for automation.
What you need
- Node.js 22.13 or newer.
- Web services and REST enabled, and an external service with the Moodle functions you will use.
- A token for a dedicated user with only the permissions it needs.
Installation
Prepare Moodle (administrator)
Enable web services and the REST protocol.
Create an external service and add the core functions you need, plus
core_webservice_get_site_infofor version detection.Create a dedicated user with the required permissions and a token for that service.
Install and configure
Install the package, or run it directly with npx:
npm install moodle-core-cliSet the Moodle address and token:
export MOODLE_BASE_URL=https://moodle.example.com export MOODLE_TOKEN=your-token
First use
Read before you write:
npx moodle-core-cli get-courses npx moodle-core-cli get-course --course-id 42Writing requires an explicit flag:
npx moodle-core-cli create-course --allow-write \ --fullname "Introduction to Biology" --shortname BIO101 --category-id 3
Good to know
- It can only do what the Moodle service exposes and what the token's user is allowed to do.