Submit a ticket My Tickets

Using the Web Services Application Programming Interface (API) in Moodle


Moodle's Web Services Application Programming Interface (API) allows external systems to perform operations that are normally only accessible from within a Moodle site. This guide will show you how to create an external service in Moodle, how to generate a token for the service, and how to call the service with a POST request from outside the Moodle system. We will create an external service that can be used to assign users to roles in various contexts throughout the Moodle site.

Before You Begin 


  1. Create an external service
  2. Add functions to the service: Assign role to user
  3. Assign role to user
  4. Obtain an access token
  5. Create a token
  6. Call the API from an external service

Creating an External Service 

  1. On your Moodle site, click Site Administration, then Plugins, then Web services and then External services
  2. Click the Add link at the bottom of the page beneath the custom services heading
  3. Type a name for your service that describes what it can do in basic terms. Check the Enabled and Authorized users only boxes and click the Add service button. 
  4. You will be taken to the new external service's page. Before your service can be used, you must associate one or more functions with it. Click the Add functions link. 

Adding Functions to the Service “Assign Role to User”

  1. Search for the function(s) you wish to use with this external service by name in the search box, and then click the name of the function to add it to the service
    • Note: You may add as many functions as you like to the service, but it is best to include only functions that are necessary to your use of this service. For a complete list of available functions, visit the Web Services API documentation page on your Moodle site by clicking the Site Administration, then Plugins, then Web Services, and then API Documentation

Assign Role to User

  1. Return to the External services page and you should see the service you just created listed under the Custom services heading. Click the Authorized users link.     a. Note: Now you must connect this service with a user in the system. The user will run the function(s) on behalf of the external service, and the user's name will appear in the Moodle system logs for the action. For this reason, you may want to create a new user for the purpose of running Web Service functions so it is clear which user is responsible for which actions on the site logs. The user you choose to create must have the correct permissions to perform the action in the system, so it may make sense to assign your Web Services user to a high-level system role such as Manager or Site Administrator.
  2. Select the authorized user and click Add. If the user is missing any permissions to perform the service's functions, those permissions will be listed at the bottom of this page.  
  3. Navigate to Site Administration, then Plugins, then Web services, and then Manage protocols
  4. Make sure that the REST protocol is enabled
    • If it is disabled, click the eye icon next to it to enable it
  5. The external service is now properly configured

Obtaining an Access Token

Before you can use the external service, you must create an access token. This token forms part of your external API call and functions as a password that proves your call has the right to manipulate data on your Moodle site.

  1. Navigate to Site Administration, then Plugins, then Web services, and then Manage tokens 
  2. Click the Add link at the bottom of the page
  3. Select the authorized user you chose in the previous section and choose the appropriate external service from the drop-down menu
  4. Click the Save changes button
  5. You will now see your new access token listed 
  6. Copy the token for use in the next section

Calling the API from an External Service

To access the external service you created earlier, you need to send a POST request to the appropriate uniform resource locator (URL) for your Moodle site. The URL format is:


The necessary parameters are:

Note: You can pass the parameters of your request in x-www-form-encoded format. These parameters are listed in the API documentation for the function you are using.

Now you are ready to use the Web Services API in Moodle. 

*Add important callouts here

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.