Google Docs to Forms - Formswrite.com logo
BlogPricing

Moodle API: Complete Guide with Examples (REST, Authentication, Endpoints)

Formswrite Team

February 15, 2026

Moodle API: Complete Guide with Examples (REST, Authentication, Endpoints)

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:
ProtocolURL PatternResponse Format
REST/webservice/rest/server.phpJSON or XML
SOAP/webservice/soap/server.phpXML
XML-RPC/webservice/xmlrpc/server.phpXML
The REST API is the most commonly used and recommended approach.

Moodle API Authentication

Step 1: Enable Web Services

  1. Log in as a Moodle administrator
  2. Go to Site AdministrationAdvanced features
  3. Check "Enable web services" and save

Step 2: Enable the REST Protocol

  1. Go to Site AdministrationPluginsWeb servicesManage protocols
  2. Enable REST protocol

Step 3: Create an API Token

There are two ways to authenticate:

Option A: User Token (Recommended for Testing)

  1. Go to Site AdministrationPluginsWeb servicesManage tokens
  2. Click "Create token"
  3. Select the user and the service
  4. 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

  1. Go to Site AdministrationPluginsWeb servicesExternal services
  2. Click "Add"
  3. Name your service and add the functions you need
  4. 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

FunctionDescription
core_user_create_usersCreate new users
core_user_get_usersSearch for users
core_user_get_users_by_fieldGet users by field (email, username, id)
core_user_update_usersUpdate user profiles
core_user_delete_usersDelete users

Course Management

FunctionDescription
core_course_get_coursesGet all courses
core_course_create_coursesCreate new courses
core_course_update_coursesUpdate course settings
core_course_get_contentsGet course content (sections, activities)
core_course_get_categoriesGet course categories

Enrolment

FunctionDescription
enrol_manual_enrol_usersEnrol users in courses
core_enrol_get_enrolled_usersGet enrolled users
enrol_manual_unenrol_usersRemove users from courses

Grades

FunctionDescription
gradereport_user_get_grade_itemsGet grade items for a user
core_grades_get_gradesGet grades for activities
gradereport_overview_get_course_gradesGet course grade overview

Quiz

FunctionDescription
mod_quiz_get_quizzes_by_coursesGet quizzes in courses
mod_quiz_get_user_attemptsGet user quiz attempts
mod_quiz_get_attempt_reviewGet 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:
TaskMoodle APIFormswrite API
Create quiz from documentNot supportedSingle API call
AI question extractionNot supportedBuilt-in
Generate Moodle XMLNot supportedAutomatic
Detect question typesManualAI-powered

How Formswrite Works with Moodle

  1. Call the Formswrite API with your Google Doc and format: "moodle"
  2. Download the generated Moodle XML file
  3. 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.

Share this post with your network


Formswrite - Google Docs to Forms Converter

Create forms in seconds, not hours

Convert Google Docs to Forms with one click

Save hours on quiz creation every month.

No more manual form creation. No more formatting hassles.

We care about your data in our privacy policy

© 2026 Formswrite. All Rights Reserved.