Moodle API: Complete Guide with Examples (REST, Authentication, Endpoints)
Formswrite Team
•
February 15, 2026

Moodle API: Complete Guide with Examples
The Moodle API (also known as the Moodle Web Services API or Moodle REST API) allows developers to interact with Moodle programmatically. You can create users, enrol students, retrieve grades, manage courses, and much more — all through HTTP requests.
This guide covers everything you need to get started with the Moodle API.
What Is the Moodle API?
The Moodle API is a set of web service functions exposed over HTTP. It supports multiple protocols:
| Protocol | URL Pattern | Response Format |
|---|---|---|
| REST | /webservice/rest/server.php | JSON or XML |
| SOAP | /webservice/soap/server.php | XML |
| XML-RPC | /webservice/xmlrpc/server.php | XML |
The REST API is the most commonly used and recommended approach.
Moodle API Authentication
Step 1: Enable Web Services
- Log in as a Moodle administrator
- Go to Site Administration → Advanced features
- Check "Enable web services" and save
Step 2: Enable the REST Protocol
- Go to Site Administration → Plugins → Web services → Manage protocols
- Enable REST protocol
Step 3: Create an API Token
There are two ways to authenticate:
Option A: User Token (Recommended for Testing)
- Go to Site Administration → Plugins → Web services → Manage tokens
- Click "Create token"
- Select the user and the service
- Save — you'll receive a token string
Option B: Token via Login
bashcurl "https://your-moodle.com/login/token.php?username=admin&password=yourpassword&service=moodle_mobile_app"
Response:
json{
"token": "abc123def456..."
}
Step 4: Create a Web Service
- Go to Site Administration → Plugins → Web services → External services
- Click "Add"
- Name your service and add the functions you need
- Assign an authorized user
How to Use the Moodle API
Base URL Format
https://your-moodle.com/webservice/rest/server.php?wstoken=YOUR_TOKEN&wsfunction=FUNCTION_NAME&moodlewsrestformat=json
Example: Get Site Info
bashcurl "https://your-moodle.com/webservice/rest/server.php?wstoken=YOUR_TOKEN&wsfunction=core_webservice_get_site_info&moodlewsrestformat=json"
Response:
json{
"sitename": "My Moodle Site",
"username": "admin",
"firstname": "Admin",
"lastname": "User",
"lang": "en",
"userid": 2,
"siteurl": "https://your-moodle.com",
"release": "4.3 (Build: 20231009)",
"version": "2023100900"
}
Key Moodle API Endpoints
User Management
| Function | Description |
|---|---|
core_user_create_users | Create new users |
core_user_get_users | Search for users |
core_user_get_users_by_field | Get users by field (email, username, id) |
core_user_update_users | Update user profiles |
core_user_delete_users | Delete users |
Course Management
| Function | Description |
|---|---|
core_course_get_courses | Get all courses |
core_course_create_courses | Create new courses |
core_course_update_courses | Update course settings |
core_course_get_contents | Get course content (sections, activities) |
core_course_get_categories | Get course categories |
Enrolment
| Function | Description |
|---|---|
enrol_manual_enrol_users | Enrol users in courses |
core_enrol_get_enrolled_users | Get enrolled users |
enrol_manual_unenrol_users | Remove users from courses |
Grades
| Function | Description |
|---|---|
gradereport_user_get_grade_items | Get grade items for a user |
core_grades_get_grades | Get grades for activities |
gradereport_overview_get_course_grades | Get course grade overview |
Quiz
| Function | Description |
|---|---|
mod_quiz_get_quizzes_by_courses | Get quizzes in courses |
mod_quiz_get_user_attempts | Get user quiz attempts |
mod_quiz_get_attempt_review | Get attempt review data |
Moodle REST API Examples
Create a User
bashcurl -X POST "https://your-moodle.com/webservice/rest/server.php" \
-d "wstoken=YOUR_TOKEN" \
-d "wsfunction=core_user_create_users" \
-d "moodlewsrestformat=json" \
-d "users[0][username]=jdoe" \
-d "users[0][password]=SecurePass123!" \
-d "users[0][firstname]=John" \
-d "users[0][lastname]=Doe" \
-d "users[0][email][email protected]"
Get All Courses
bashcurl "https://your-moodle.com/webservice/rest/server.php?wstoken=YOUR_TOKEN&wsfunction=core_course_get_courses&moodlewsrestformat=json"
Enrol a User in a Course
bashcurl -X POST "https://your-moodle.com/webservice/rest/server.php" \
-d "wstoken=YOUR_TOKEN" \
-d "wsfunction=enrol_manual_enrol_users" \
-d "moodlewsrestformat=json" \
-d "enrolments[0][roleid]=5" \
-d "enrolments[0][userid]=3" \
-d "enrolments[0][courseid]=2"
Role IDs: 1=Manager, 3=Teacher, 5=Student
Get Enrolled Users
bashcurl "https://your-moodle.com/webservice/rest/server.php?wstoken=YOUR_TOKEN&wsfunction=core_enrol_get_enrolled_users&moodlewsrestformat=json&courseid=2"
Python Examples
Create Users in Bulk
pythonimport requests
MOODLE_URL = "https://your-moodle.com/webservice/rest/server.php"
TOKEN = "your_moodle_api_token"
users = [
{"username": "student1", "firstname": "Alice", "lastname": "Smith", "email": "[email protected]", "password": "Pass123!"},
{"username": "student2", "firstname": "Bob", "lastname": "Jones", "email": "[email protected]", "password": "Pass456!"},
]
params = {
"wstoken": TOKEN,
"wsfunction": "core_user_create_users",
"moodlewsrestformat": "json",
}
for i, user in enumerate(users):
for key, value in user.items():
params[f"users[{i}][{key}]"] = value
response = requests.post(MOODLE_URL, data=params)
print(response.json())
Get All Courses
pythonresponse = requests.get(MOODLE_URL, params={
"wstoken": TOKEN,
"wsfunction": "core_course_get_courses",
"moodlewsrestformat": "json"
})
courses = response.json()
for course in courses:
print(f"{course['id']}: {course['fullname']}")
Importing Quizzes: Moodle API vs Formswrite API
The Moodle API is powerful for managing users, courses, and enrolments. However, creating quiz questions through the Moodle API is complex — there's no simple "create quiz from document" endpoint.
For quiz creation, Formswrite provides a much simpler approach:
| Task | Moodle API | Formswrite API |
|---|---|---|
| Create quiz from document | Not supported | Single API call |
| AI question extraction | Not supported | Built-in |
| Generate Moodle XML | Not supported | Automatic |
| Detect question types | Manual | AI-powered |
How Formswrite Works with Moodle
- Call the Formswrite API with your Google Doc and
format: "moodle" - Download the generated Moodle XML file
- Import into Moodle via Question Bank → Import
bash# Convert Google Doc to Moodle quiz
curl -X POST https://api.formswrite.com/api/v1/convert \
-H "Authorization: Bearer FORMSWRITE_TOKEN" \
-H "Content-Type: application/json" \
-d '{"documentId": "YOUR_DOC_ID", "format": "moodle"}'
This is ideal for educators who already have quiz content in Google Docs and want to get it into Moodle quickly.
Moodle API Documentation Resources
- Official docs: docs.moodle.org/dev/Web_services
- Function reference: Your Moodle site → Site Administration → Plugins → Web services → API Documentation
- Testing tool: Your Moodle site → Site Administration → Development → Web service test client
FAQ
What is the Moodle REST API?
The Moodle REST API is the HTTP-based interface for interacting with Moodle programmatically. You send GET or POST requests to specific function endpoints and receive JSON responses.
How do I get a Moodle API token?
Go to Site Administration → Plugins → Web services → Manage tokens → Create token. Or use the
login/token.php endpoint with username and password.What can I do with the Moodle API?
You can manage users, courses, enrolments, grades, assignments, forums, and most other Moodle features programmatically.
Is the Moodle API free?
Yes. The Moodle API is part of the core Moodle platform and is free to use with any Moodle installation.
Can I create quiz questions via the Moodle API?
The Moodle API has limited support for quiz question creation. For bulk quiz creation from documents, use Formswrite to generate Moodle XML files, then import them through Moodle's Question Bank.
Summary
The Moodle API is essential for automating user management, course operations, and integrations. For quiz creation specifically, pair it with Formswrite to convert Google Docs into Moodle-ready quiz files automatically.