<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Metasismedia</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Metasismedia"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Metasismedia"/>
	<updated>2026-04-19T00:29:48Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Web_services_API&amp;diff=52787</id>
		<title>Web services API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Web_services_API&amp;diff=52787"/>
		<updated>2017-07-31T14:45:29Z</updated>

		<summary type="html">&lt;p&gt;Metasismedia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Web services API allows you to expose your plugin&#039;s functions (usually [[External functions API|external functions]]) as Web services.&lt;br /&gt;
&lt;br /&gt;
Once you have done this, your plugin&#039;s functions will be accessible to other systems through Web services using one of a number of protocols, like XML-RPC, REST or SOAP.&lt;br /&gt;
&lt;br /&gt;
Exposing functions as Web service functions is done in one file called services.php.&lt;br /&gt;
&lt;br /&gt;
== services.php ==&lt;br /&gt;
* This file can be added to the &#039;&#039;&#039;db&#039;&#039;&#039; sub-folder of your [[Frankenstyle#Plugin_types|plugin]].&lt;br /&gt;
* This file contains one or two arrays. The first array declares your web service functions. Each of these declarations reference a function in your module (usually [[External functions API|an external function]]).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
$functions = array(&lt;br /&gt;
        &#039;local_PLUGINNAME_FUNCTIONNAME&#039; =&amp;gt; array( // local_PLUGINNAME_FUNCTIONNAME is the name of the web service function that the client will call.                                                                                &lt;br /&gt;
                &#039;classname&#039;   =&amp;gt; &#039;local_PLUGINNAME_external&#039;, // create this class in local/PLUGINNAME/externallib.php&lt;br /&gt;
                &#039;methodname&#039;  =&amp;gt; &#039;FUNCTIONNAME&#039;, // implement this function into the above class&lt;br /&gt;
                &#039;classpath&#039;   =&amp;gt; &#039;local/PLUGINNAME/externallib.php&#039;,&lt;br /&gt;
                &#039;description&#039; =&amp;gt; &#039;This documentation will be displayed in the generated API documentation &lt;br /&gt;
                                          (Administration &amp;gt; Plugins &amp;gt; Webservices &amp;gt; API documentation)&#039;,&lt;br /&gt;
                &#039;type&#039;        =&amp;gt; &#039;write&#039;, // the value is &#039;write&#039; if your function does any database change, otherwise it is &#039;read&#039;.&lt;br /&gt;
                &#039;ajax&#039;        =&amp;gt; true, // true/false if you allow this web service function to be callable via ajax&lt;br /&gt;
                &#039;capabilities&#039;  =&amp;gt; &#039;moodle/xxx:yyy, addon/xxx:yyy&#039;,  // List the capabilities used in the function (missing capabilities are displayed for authorised users and also for manually created tokens in the web interface, this is just informative).&lt;br /&gt;
                &#039;services&#039; =&amp;gt; array(MOODLE_OFFICIAL_MOBILE_SERVICE)    // Optional, only available for Moodle 3.1 onwards. List of built-in services (by shortname) where the function will be included. Services created manually via the Moodle interface are not supported.&lt;br /&gt;
        )&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The second, optional array declares the pre-built services.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// OPTIONAL&lt;br /&gt;
// During the plugin installation/upgrade, Moodle installs these services as pre-build services. &lt;br /&gt;
// A pre-build service is not editable by administrator.&lt;br /&gt;
$services = array(&lt;br /&gt;
        &#039;MY SERVICE&#039; =&amp;gt; array(&lt;br /&gt;
                &#039;functions&#039; =&amp;gt; array (&#039;local_PLUGINNAME_FUNCTIONNAME&#039;), &lt;br /&gt;
                &#039;restrictedusers&#039; =&amp;gt; 0, // if 1, the administrator must manually select which user can use this service. &lt;br /&gt;
                                                   // (Administration &amp;gt; Plugins &amp;gt; Web services &amp;gt; Manage services &amp;gt; Authorised users)&lt;br /&gt;
                &#039;enabled&#039;=&amp;gt;1, // if 0, then token linked to this service won&#039;t work&lt;br /&gt;
        )&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;Don&#039;t forget to increment the version number in the version.php file of your plugin whenever services.php changes, otherwise Moodle will not detect the changes.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Detailed tutorial ==&lt;br /&gt;
&lt;br /&gt;
A more detailed tutorial for this system can be found at the following page:&lt;br /&gt;
* [[Adding a web service to a plugin]]&lt;br /&gt;
&lt;br /&gt;
Among other things, that tutorial explains how you define the parameters and return value of your function.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
You will find an example of a services.php file in the [https://github.com/moodlehq/moodle-local_wstemplate web service template plugin]. This plugin contains a web service hello_world function.&lt;br /&gt;
&lt;br /&gt;
Also, to make testing easy for you, the plugin is distributed with a test client in the &#039;&#039;&#039;client&#039;&#039;&#039; folder.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Core APIs]]&lt;br /&gt;
* [[External functions API]]&lt;br /&gt;
* [[Web services API Changes]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Web_Services]]&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Metasismedia</name></author>
	</entry>
</feed>