core/v2/namespaces

NOTE: Requests to core/v2/namespaces API endpoints require you to authenticate with a Sensu API key or access token. The code examples in this document use the environment variable $SENSU_API_KEY to represent a valid API key in API requests.

Get all namespaces

The /namespaces API endpoint provides HTTP GET access to namespace data.

Example

The following example demonstrates a GET request to the /namespaces API endpoint:

curl -X GET \
http://127.0.0.1:8080/api/core/v2/namespaces \
-H "Authorization: Key $SENSU_API_KEY"

The request results in a successful HTTP/1.1 200 OK response and a JSON array that contains namespace definitions:

[
  {
    "name": "default"
  },
  {
    "name": "development"
  }
]

API Specification

/namespaces (GET)
description Returns the list of namespaces.
example url http://hostname:8080/api/core/v2/namespaces
pagination This endpoint supports pagination using the limit and continue query parameters.
response filtering This endpoint supports API response filtering.
response type Array
response codes
  • Success: 200 (OK)
  • Error: 500 (Internal Server Error)
output
[
  {
    "name": "default"
  },
  {
    "name": "development"
  }
]

Create a new namespace

The /namespaces API endpoint provides HTTP POST access to create Sensu namespaces.

Example

In the following example, an HTTP POST request is submitted to the /namespaces API endpoint to create the namespace development:

curl -X POST \
-H "Authorization: Key $SENSU_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "name": "development"
}' \
http://127.0.0.1:8080/api/core/v2/namespaces

The request will return a successful HTTP/1.1 201 Created response.

API Specification

/namespaces (POST)
description Creates a Sensu namespace.
example URL http://hostname:8080/api/core/v2/namespaces
payload
{
  "name": "development"
}
response codes
  • Success: 201 (Created)
  • Malformed: 400 (Bad Request)
  • Error: 500 (Internal Server Error)

Create or update a namespace

The /namespaces/:namespace API endpoint provides HTTP PUT access to create or update specific Sensu namespaces, by namespace name.

Example

In the following example, an HTTP PUT request is submitted to the /namespaces/:namespace API endpoint to create the namespace development:

curl -X PUT \
-H "Authorization: Key $SENSU_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "name": "development"
}' \
http://127.0.0.1:8080/api/core/v2/namespaces/development

The request will return a successful HTTP/1.1 201 Created response.

API Specification

/namespaces/:namespace (PUT)
description Creates or updates a Sensu namespace.
example URL http://hostname:8080/api/core/v2/namespaces/development
payload
{
  "name": "development"
}
response codes
  • Success: 201 (Created)
  • Malformed: 400 (Bad Request)
  • Error: 500 (Internal Server Error)

Delete a namespace

The /namespaces/:namespace API endpoint provides HTTP DELETE access to delete a namespace from Sensu (specified by the namespace name).

Example

The following example shows a request to the /namespaces/:namespace API endpoint to delete the namespace development, resulting in a successful HTTP/1.1 204 No Content response.

curl -X DELETE \
http://127.0.0.1:8080/api/core/v2/namespaces/development \
-H "Authorization: Key $SENSU_API_KEY"

Namespaces must be empty before you can delete them. If the response to your delete request includes Error: resource is invalid: namespace is not empty, the namespace may still contain events or other resources. To remove all resources and events so that you can delete a namespace, use this sensuctl dump command (replace <namespace-name> with the namespace you want to empty):

sensuctl dump entities,events,assets,checks,filters,handlers,secrets/v1.Secret --namespace <namespace-name> | sensuctl delete

API Specification

/namespaces/:namespace (DELETE)
description Removes the specified namespace from Sensu.
example url http://hostname:8080/api/core/v2/namespaces/development
response codes
  • Success: 204 (No Content)
  • Missing: 404 (Not Found)
  • Error: 500 (Internal Server Error)

Get a subset of namespaces with response filtering

The /namespaces API endpoint supports response filtering for a subset of namespace data based on labels and the field namespace.name.

Example

The following example demonstrates a request to the /namespaces API endpoint with response filtering for only the namespace definitions for the production namespace:

curl -H "Authorization: Key $SENSU_API_KEY" http://127.0.0.1:8080/api/core/v2/namespaces -G \
--data-urlencode 'fieldSelector=namespace.name == production'

The example request will result in a successful HTTP/1.1 200 OK response and a JSON array that contains only namespace definitions for the production namespace:

[
  {
    "name": "production"
  }
]

NOTE: Read API response filtering for more filter statement examples that demonstrate how to filter responses using different operators with label and field selectors.

API Specification

/namespaces (GET) with response filters
description Returns the list of namespaces that match the response filters applied in the API request.
example url http://hostname:8080/api/core/v2/namespaces
pagination This endpoint supports pagination using the limit and continue query parameters.
response type Array
response codes
  • Success: 200 (OK)
  • Error: 500 (Internal Server Error)
output
[
  {
    "name": "production"
  }
]

Get all namespaces for a specific user

COMMERCIAL FEATURE: Access the /user-namespaces API endpoint in the packaged Sensu Go distribution. For more information, read Get started with commercial features.

The /user-namespaces API endpoint provides HTTP GET access to the namespaces the current user can access.

Example

The following example demonstrates a GET request to the /user-namespaces API endpoint:

curl -X GET \
http://127.0.0.1:8080/api/enterprise/user-namespaces \
-H "Authorization: Key $SENSU_API_KEY"

The example request will result in a successful HTTP/1.1 200 OK response and a JSON array that contains only the namespaces the current user can access:

[
  {
    "name": "default"
  },
  {
    "name": "development"
  }
]

API Specification

/user-namespaces (GET)
description Returns the list of namespaces a user has access to.
example url http://hostname:8080/api/enterprise/user-namespaces
response type Array
response codes
  • Success: 200 (OK)
  • Error: 500 (Internal Server Error)
output
[
  {
    "name": "default"
  },
  {
    "name": "development"
  }
]