Title: rest_api_loaded
Published: December 9, 2015
Last modified: February 24, 2026

---

# rest_api_loaded()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#wp--skip-link--target)

Loads the REST API.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#source)󠁿

    ```php
    function rest_api_loaded() {
    	if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
    		return;
    	}

    	// Return an error message if query_var is not a string.
    	if ( ! is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
    		$rest_type_error = new WP_Error(
    			'rest_path_invalid_type',
    			__( 'The REST route parameter must be a string.' ),
    			array( 'status' => 400 )
    		);
    		wp_die( $rest_type_error );
    	}

    	/**
    	 * Whether this is a REST Request.
    	 *
    	 * @since 4.4.0
    	 * @var bool
    	 */
    	define( 'REST_REQUEST', true );

    	// Initialize the server.
    	$server = rest_get_server();

    	// Fire off the request.
    	$route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] );
    	if ( empty( $route ) ) {
    		$route = '/';
    	}
    	$server->serve_request( $route );

    	// We're done.
    	die();
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/rest-api.php#L436)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/rest-api.php#L436-L471)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#related)󠁿

| Uses | Description | 
| [rest_get_server()](https://developer.wordpress.org/reference/functions/rest_get_server/)`wp-includes/rest-api.php` |

Retrieves the current REST server instance.

  | 
| [untrailingslashit()](https://developer.wordpress.org/reference/functions/untrailingslashit/)`wp-includes/formatting.php` |

Removes trailing forward slashes and backslashes if they exist.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [wp_die()](https://developer.wordpress.org/reference/functions/wp_die/)`wp-includes/functions.php` |

Kills WordPress execution and displays HTML page with an error message.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/rest_api_loaded/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Frest_api_loaded%2F)
before being able to contribute a note or feedback.