---
title: Create a new rule
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > API Reference > Scorecards
---

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

# Create a new rule{% #create-a-new-rule %}
Copy pageCopied
{% tab title="v2" %}

| Datadog site      | API endpoint                                              |
| ----------------- | --------------------------------------------------------- |
| ap1.datadoghq.com | POST https://api.ap1.datadoghq.com/api/v2/scorecard/rules |
| ap2.datadoghq.com | POST https://api.ap2.datadoghq.com/api/v2/scorecard/rules |
| app.datadoghq.eu  | POST https://api.datadoghq.eu/api/v2/scorecard/rules      |
| app.ddog-gov.com  | POST https://api.ddog-gov.com/api/v2/scorecard/rules      |
| us2.ddog-gov.com  | POST https://api.us2.ddog-gov.com/api/v2/scorecard/rules  |
| app.datadoghq.com | POST https://api.datadoghq.com/api/v2/scorecard/rules     |
| us3.datadoghq.com | POST https://api.us3.datadoghq.com/api/v2/scorecard/rules |
| us5.datadoghq.com | POST https://api.us5.datadoghq.com/api/v2/scorecard/rules |

### Overview

Creates a new rule.

OAuth apps require the `apm_service_catalog_write` authorization [scope](https://docs.datadoghq.com/api/latest/scopes.md#scorecards) to access this endpoint.



### Request

#### Body Data (required)

Rule attributes.

{% tab title="Model" %}

| Parent field | Field          | Type    | Description                                                                                                       |
| ------------ | -------------- | ------- | ----------------------------------------------------------------------------------------------------------------- |
|              | data           | object  | Scorecard create rule request data.                                                                               |
| data         | attributes     | object  | Attributes for creating or updating a rule. Server-managed fields (created_at, modified_at, custom) are excluded. |
| attributes   | description    | string  | Explanation of the rule.                                                                                          |
| attributes   | enabled        | boolean | If enabled, the rule is calculated as part of the score.                                                          |
| attributes   | level          | int32   | The maturity level of the rule (1, 2, or 3).                                                                      |
| attributes   | name           | string  | Name of the rule.                                                                                                 |
| attributes   | owner          | string  | Owner of the rule.                                                                                                |
| attributes   | scope_query    | string  | A query to filter which entities this rule applies to.                                                            |
| attributes   | scorecard_name | string  | The scorecard name to which this rule must belong.                                                                |
| data         | type           | enum    | The JSON:API type for scorecard rules. Allowed enum values: `rule`                                                |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "enabled": true,
      "name": "Example-Scorecard",
      "scorecard_name": "Observability Best Practices"
    },
    "type": "rule"
  }
}
```

{% /tab %}

### Response

{% tab title="201" %}
Created
{% tab title="Model" %}
Created rule in response.

| Parent field  | Field          | Type      | Description                                                        |
| ------------- | -------------- | --------- | ------------------------------------------------------------------ |
|               | data           | object    | Create rule response data.                                         |
| data          | attributes     | object    | Details of a rule.                                                 |
| attributes    | category       | string    | **DEPRECATED**: The scorecard name to which this rule must belong. |
| attributes    | created_at     | date-time | Creation time of the rule outcome.                                 |
| attributes    | custom         | boolean   | Defines if the rule is a custom rule.                              |
| attributes    | description    | string    | Explanation of the rule.                                           |
| attributes    | enabled        | boolean   | If enabled, the rule is calculated as part of the score.           |
| attributes    | level          | int32     | The maturity level of the rule (1, 2, or 3).                       |
| attributes    | modified_at    | date-time | Time of the last rule outcome modification.                        |
| attributes    | name           | string    | Name of the rule.                                                  |
| attributes    | owner          | string    | Owner of the rule.                                                 |
| attributes    | scope_query    | string    | A query to filter which entities this rule applies to.             |
| attributes    | scorecard_name | string    | The scorecard name to which this rule must belong.                 |
| data          | id             | string    | The unique ID for a scorecard rule.                                |
| data          | relationships  | object    | Scorecard create rule response relationship.                       |
| relationships | scorecard      | object    | Relationship data for a rule.                                      |
| scorecard     | data           | object    | Rule relationship data.                                            |
| data          | id             | string    | The unique ID for a scorecard.                                     |
| data          | type           | enum      | The JSON:API type for scorecard. Allowed enum values: `scorecard`  |
| data          | type           | enum      | The JSON:API type for scorecard rules. Allowed enum values: `rule` |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "category": "string",
      "created_at": "2019-09-19T10:00:00.000Z",
      "custom": false,
      "description": "string",
      "enabled": true,
      "level": 2,
      "modified_at": "2019-09-19T10:00:00.000Z",
      "name": "Team Defined",
      "owner": "string",
      "scope_query": "kind:service",
      "scorecard_name": "Deployments automated via Deployment Trains"
    },
    "id": "q8MQxk8TCqrHnWkx",
    "relationships": {
      "scorecard": {
        "data": {
          "id": "q8MQxk8TCqrHnWkp",
          "type": "scorecard"
        }
      }
    },
    "type": "rule"
  }
}
```

{% /tab %}

{% /tab %}

{% tab title="400" %}
Bad Request
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="403" %}
Forbidden
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="429" %}
Too many requests
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

### Code Example

##### 
                          \## default
# 
 \# Curl command curl -X POST "https://api.datadoghq.com/api/v2/scorecard/rules" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" \
-d @- << EOF
{
  "data": {
    "attributes": {
      "enabled": true,
      "name": "My Rule",
      "owner": "Datadog",
      "scorecard_name": "My Scorecard"
    },
    "type": "rule"
  }
}
EOF 
                        
##### 

```go
// Create a new rule returns "Created" response

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
	"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)

func main() {
	body := datadogV2.CreateRuleRequest{
		Data: &datadogV2.CreateRuleRequestData{
			Attributes: &datadogV2.RuleAttributesRequest{
				Enabled:       datadog.PtrBool(true),
				Name:          datadog.PtrString("Example-Scorecard"),
				ScorecardName: datadog.PtrString("Observability Best Practices"),
			},
			Type: datadogV2.RULETYPE_RULE.Ptr(),
		},
	}
	ctx := datadog.NewDefaultContext(context.Background())
	configuration := datadog.NewConfiguration()
	apiClient := datadog.NewAPIClient(configuration)
	api := datadogV2.NewScorecardsApi(apiClient)
	resp, r, err := api.CreateScorecardRule(ctx, body)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `ScorecardsApi.CreateScorecardRule`: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}

	responseContent, _ := json.MarshalIndent(resp, "", "  ")
	fmt.Fprintf(os.Stdout, "Response from `ScorecardsApi.CreateScorecardRule`:\n%s\n", responseContent)
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=go) and then save the example to `main.go` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" go run "main.go"
##### 

```java
// Create a new rule returns "Created" response

import com.datadog.api.client.ApiClient;
import com.datadog.api.client.ApiException;
import com.datadog.api.client.v2.api.ScorecardsApi;
import com.datadog.api.client.v2.model.CreateRuleRequest;
import com.datadog.api.client.v2.model.CreateRuleRequestData;
import com.datadog.api.client.v2.model.CreateRuleResponse;
import com.datadog.api.client.v2.model.RuleAttributesRequest;
import com.datadog.api.client.v2.model.RuleType;

public class Example {
  public static void main(String[] args) {
    ApiClient defaultClient = ApiClient.getDefaultApiClient();
    ScorecardsApi apiInstance = new ScorecardsApi(defaultClient);

    CreateRuleRequest body =
        new CreateRuleRequest()
            .data(
                new CreateRuleRequestData()
                    .attributes(
                        new RuleAttributesRequest()
                            .enabled(true)
                            .name("Example-Scorecard")
                            .scorecardName("Observability Best Practices"))
                    .type(RuleType.RULE));

    try {
      CreateRuleResponse result = apiInstance.createScorecardRule(body);
      System.out.println(result);
    } catch (ApiException e) {
      System.err.println("Exception when calling ScorecardsApi#createScorecardRule");
      System.err.println("Status code: " + e.getCode());
      System.err.println("Reason: " + e.getResponseBody());
      System.err.println("Response headers: " + e.getResponseHeaders());
      e.printStackTrace();
    }
  }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=java) and then save the example to `Example.java` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" java "Example.java"
##### 

```python
"""
Create a new rule returns "Created" response
"""

from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v2.api.scorecards_api import ScorecardsApi
from datadog_api_client.v2.model.create_rule_request import CreateRuleRequest
from datadog_api_client.v2.model.create_rule_request_data import CreateRuleRequestData
from datadog_api_client.v2.model.rule_attributes_request import RuleAttributesRequest
from datadog_api_client.v2.model.rule_type import RuleType

body = CreateRuleRequest(
    data=CreateRuleRequestData(
        attributes=RuleAttributesRequest(
            enabled=True,
            name="Example-Scorecard",
            scorecard_name="Observability Best Practices",
        ),
        type=RuleType.RULE,
    ),
)

configuration = Configuration()
with ApiClient(configuration) as api_client:
    api_instance = ScorecardsApi(api_client)
    response = api_instance.create_scorecard_rule(body=body)

    print(response)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=python) and then save the example to `example.py` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" python3 "example.py"
##### 

```ruby
# Create a new rule returns "Created" response

require "datadog_api_client"
api_instance = DatadogAPIClient::V2::ScorecardsAPI.new

body = DatadogAPIClient::V2::CreateRuleRequest.new({
  data: DatadogAPIClient::V2::CreateRuleRequestData.new({
    attributes: DatadogAPIClient::V2::RuleAttributesRequest.new({
      enabled: true,
      name: "Example-Scorecard",
      scorecard_name: "Observability Best Practices",
    }),
    type: DatadogAPIClient::V2::RuleType::RULE,
  }),
})
p api_instance.create_scorecard_rule(body)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=ruby) and then save the example to `example.rb` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" rb "example.rb"
##### 

```rust
// Create a new rule returns "Created" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_scorecards::ScorecardsAPI;
use datadog_api_client::datadogV2::model::CreateRuleRequest;
use datadog_api_client::datadogV2::model::CreateRuleRequestData;
use datadog_api_client::datadogV2::model::RuleAttributesRequest;
use datadog_api_client::datadogV2::model::RuleType;

#[tokio::main]
async fn main() {
    let body = CreateRuleRequest::new().data(
        CreateRuleRequestData::new()
            .attributes(
                RuleAttributesRequest::new()
                    .enabled(true)
                    .name("Example-Scorecard".to_string())
                    .scorecard_name("Observability Best Practices".to_string()),
            )
            .type_(RuleType::RULE),
    );
    let configuration = datadog::Configuration::new();
    let api = ScorecardsAPI::with_config(configuration);
    let resp = api.create_scorecard_rule(body).await;
    if let Ok(value) = resp {
        println!("{:#?}", value);
    } else {
        println!("{:#?}", resp.unwrap_err());
    }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=rust) and then save the example to `src/main.rs` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" cargo run
##### 

```typescript
/**
 * Create a new rule returns "Created" response
 */

import { client, v2 } from "@datadog/datadog-api-client";

const configuration = client.createConfiguration();
const apiInstance = new v2.ScorecardsApi(configuration);

const params: v2.ScorecardsApiCreateScorecardRuleRequest = {
  body: {
    data: {
      attributes: {
        enabled: true,
        name: "Example-Scorecard",
        scorecardName: "Observability Best Practices",
      },
      type: "rule",
    },
  },
};

apiInstance
  .createScorecardRule(params)
  .then((data: v2.CreateRuleResponse) => {
    console.log(
      "API called successfully. Returned data: " + JSON.stringify(data)
    );
  })
  .catch((error: any) => console.error(error));
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=typescript) and then save the example to `example.ts` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" tsc "example.ts"
{% /tab %}
