Title: WP_REST_Server::get_namespace_index
Published: December 9, 2015
Last modified: May 20, 2026

---

# WP_REST_Server::get_namespace_index( WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#changelog)

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

Retrieves the index for a namespace.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#parameters)󠁿

 `$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

REST request instance.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
instance if the index was found, [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
if the namespace isn’t set.

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

    ```php
    public function get_namespace_index( $request ) {
    	$namespace = $request['namespace'];

    	if ( ! isset( $this->namespaces[ $namespace ] ) ) {
    		return new WP_Error(
    			'rest_invalid_namespace',
    			__( 'The specified namespace could not be found.' ),
    			array( 'status' => 404 )
    		);
    	}

    	$routes    = $this->namespaces[ $namespace ];
    	$endpoints = array_intersect_key( $this->get_routes(), $routes );

    	$data     = array(
    		'namespace' => $namespace,
    		'routes'    => $this->get_data_for_routes( $endpoints, $request['context'] ),
    	);
    	$response = rest_ensure_response( $data );

    	// Link to the root index.
    	$response->add_link( 'up', rest_url( '/' ) );

    	/**
    	 * Filters the REST API namespace index data.
    	 *
    	 * This typically is just the route data for the namespace, but you can
    	 * add any data you'd like here.
    	 *
    	 * @since 4.4.0
    	 *
    	 * @param WP_REST_Response $response Response data.
    	 * @param WP_REST_Request  $request  Request data. The namespace is passed as the 'namespace' parameter.
    	 */
    	return apply_filters( 'rest_namespace_index', $response, $request );
    }
    ```

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

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#hooks)󠁿

 [apply_filters( ‘rest_namespace_index’, WP_REST_Response $response, WP_REST_Request $request )](https://developer.wordpress.org/reference/hooks/rest_namespace_index/)

Filters the REST API namespace index data.

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

| Uses | Description | 
| [WP_REST_Server::get_data_for_routes()](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_routes/)`wp-includes/rest-api/class-wp-rest-server.php` |

Retrieves the publicly-visible data for routes.

  | 
| [WP_REST_Server::get_routes()](https://developer.wordpress.org/reference/classes/wp_rest_server/get_routes/)`wp-includes/rest-api/class-wp-rest-server.php` |

Retrieves the route map.

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

Ensures a REST response is a response object (for consistency).

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

Retrieves the URL to a REST endpoint.

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

Retrieves the translation of $text.

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

Calls the callback functions that have been added to a filter hook.

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

Initializes the error.

  |

[Show 5 more](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/get_namespace_index/?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%2Fclasses%2Fwp_rest_server%2Fget_namespace_index%2F)
before being able to contribute a note or feedback.