> For the complete documentation index, see [llms.txt](https://help.form.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.form.io/dev/fetch-plugin-api.md).

# Fetch Plugin API

The Fetch Plugin API provides a way for modules to hook into the request flow of API traffic coming to and from the Form.io Renderer and Builder. The following provides the documentation for this plugin.

## Plugin methods

### **Formio.registerPlugin(plugin, \[name])**

Registers a plugin with Formio.js. An optional name parameter can be provided to be used with Formio.getPlugin().

A plugin must be an object, and can have any of the following optional properties:

* `priority`: The priority of the plugin relative to other plugins that determines call order. Higher numbers have higher priority. If not specified it will default to a priority of 0.
* `init`: An initialization function called when registered with Formio. It will receive the global Formio object as its first parameter.
* `deregister`: A deregistration function called when deregistering with Formio. It will receive the global Formio object as its first parameter.

See [**below**](https://github.com/formio/formio.js/wiki/Fetch-Plugin-API#plugin-hooks) for using hooks in your plugin.

### **Formio.getPlugin(name)**

Returns the plugin registered with the given name.

### **Formio.deregisterPlugin(plugin)**

Deregisters a plugin with Formio.js. It will call the `deregister` function on the plugin before deregistering. The `plugin` argument can be the instance of the plugin or the optional name given when registered. Returns true if the plugin was successfully deregistered, and false if the plugin does not exist.

### **Formio.events**

This is an [**EventEmitter**](https://nodejs.org/api/events.html) that you may use as an event publish/subscribe system in your plugins.

## Plugin hooks

Plugins can provide hooks that are invoked at different points in the library. To use a particular hook below, add a function to your plugin with the same name as the hook.

The following are the currently available hooks.

### **preRequest(requestArgs)**

Called before a request. If you return a promise, Formio.js will wait for it to resolve before starting the request.

`requestArgs` is an object that contains the following properties:

* `formio`: The Formio instance calling the request.
* `type`: The type of resource being requested (ex: form, forms, submission).
* `url`: The url being requested.
* `method`: The HTTP request method.
* `data`: The HTTP request body, if any.
* `opts`: Any opts given to the request

### **request(requestArgs)**

Called before a request, and gives plugins a chance fulfill the request before it is sent. If you return a non-null, non-undefined value (or a promise that resolves to one), that will be used as the results of the request instead of making the default network request.

Only the first highest priority that returns a value will replace the contents. Your plugin's hook will not be called if a higher priority plugin returns a value.

`requestArgs` is an object that contains the following properties:

* `formio`: The Formio instance calling the request.
* `type`: The type of resource being requested (ex: form, forms, submission).
* `url`: The url being requested.
* `method`: The HTTP request method.
* `data`: The HTTP request body, if any.
* `opts`: Any opts given to the request

### **wrapRequestPromise(promise, requestArgs)**

Called when a request is made and gives plugins access to the promise that is returned when a user makes a request. The promise that is returned from this hook will be returned to the user. You may wrap the original promise or extend the promise chain with this hook. (You must return a promise that uses the original promise, or the promise returned to users will not resolve as expected).

`promise` is the promise of the request.

`requestArgs` is an object that contains the following properties:

* `formio`: The Formio instance calling the request.
* `type`: The type of resource being requested (ex: form, forms, submission).
* `url`: The url being requested.
* `method`: The HTTP request method.
* `data`: The HTTP request body, if any.
* `opts`: Any opts given to the request

### **preStaticRequest(requestArgs)**

Same as `preRequest` hook but used for requests that use the global Formio object instead of a Formio instance. This includes functions like `Formio.loadProjects()`, `Formio.availableActions()`, `Formio.currentUser()`.

`requestArgs` is an object that contains the following properties:

* `url`: The url being requested.
* `method`: The HTTP request method.
* `data`: The HTTP request body, if any.

### **staticRequest(requestArgs)**

Same as `request` hook but used for requests that use the global Formio object instead of a Formio instance. This includes functions like `Formio.loadProjects()`, `Formio.availableActions()`, `Formio.currentUser()`.

`requestArgs` is an object that contains the following properties:

* `url`: The url being requested.
* `method`: The HTTP request method.
* `data`: The HTTP request body, if any.

### **wrapStaticRequestPromise(promise, requestArgs)**

Same as `wrapRequestPromise` hook but used for requests that use the global Formio object instead of a Formio instance. This includes functions like `Formio.loadProjects()`, `Formio.availableActions()`, `Formio.currentUser()`.

`promise` is the promise of the request.

`requestArgs` is an object that contains the following properties:

* `url`: The url being requested.
* `method`: The HTTP request method.
* `data`: The HTTP request body, if any.

## Example Plugin

This example plugin will delay all requests by 5 seconds

#### plugins/fetch.js

```
export const DelayPlugin = {
  priority: 0,
  preRequest: function(requestArgs) {
    return new Promise(function(resolve, reject){
      setTimeout(resolve, 5000);
    })
  }
}
```

#### module.js

```
import { DelayPlugin } from './plugins/fetch';
export default {
    fetch: DelayPlugin
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.form.io/dev/fetch-plugin-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
