---
title: "Create Custom Rule"
method: PUT
path: "/api/v2/static-analysis/custom/rulesets/{ruleset_name}/rules"
tags: ["Static Analysis"]
---

# Create Custom Rule

`PUT /api/v2/static-analysis/custom/rulesets/{ruleset_name}/rules`

## Request

- Base URL: `https://api.datadoghq.com`
- URL: `https://api.datadoghq.com/api/v2/static-analysis/custom/rulesets/{ruleset_name}/rules`
- Auth: API key in header `DD-API-KEY` and API key in header `DD-APPLICATION-KEY`

Create a new custom rule within a ruleset

## Path parameters

- `ruleset_name` string, required

## Request body

- CustomRuleRequest: Request body for creating or updating a custom rule.
  - `data` CustomRuleRequestData: Data object for a custom rule create or update request.
    - `attributes` CustomRuleRequestDataAttributes: Attributes for creating or updating a custom rule.
      - `name` string: Rule name
    - `id` string: Rule identifier
    - `type` 'custom_rule': Resource type

## Response `200`

Successfully created

- CustomRuleResponse: Response containing a single custom rule.
  - `data` CustomRuleResponseData, required: Data object returned in a custom rule response, including its ID, type, and attributes.
    - `attributes` CustomRule, required: A custom static analysis rule within a ruleset.
      - `created_at` string, date-time, required: Creation timestamp
      - `created_by` string, required: Creator identifier
      - `last_revision` CustomRuleRevision, required: A specific revision of a custom static analysis rule.
        - `attributes` CustomRuleRevisionAttributes, required: Attributes of a custom rule revision, including code, metadata, and test cases.
          - `arguments` Argument[], required: Rule arguments
            - `description` string, required: Base64-encoded argument description
            - `name` string, required: Base64-encoded argument name
          - `category` 'SECURITY' | 'BEST_PRACTICES' | 'CODE_STYLE' | 'ERROR_PRONE' | 'PERFORMANCE', required: Rule category
          - `checksum` string, required: Code checksum
          - `code` string, required: Rule code
          - `created_at` string, date-time, required: Creation timestamp
          - `created_by` string, required: Creator identifier
          - `creation_message` string, required: Revision creation message
          - `cve` string, nullable, required: Associated CVE
          - `cwe` string, nullable, required: Associated CWE
          - `description` string, required: Full description
          - `documentation_url` string, nullable, required: Documentation URL
          - `is_published` boolean, required: Whether the revision is published
          - `is_testing` boolean, required: Whether this is a testing revision
          - `language` 'PYTHON' | 'JAVASCRIPT' | 'TYPESCRIPT' | 'JAVA' | 'GO' | 'YAML' | 'RUBY' | 'CSHARP' | 'PHP' | 'KOTLIN' | 'SWIFT', required: Programming language
          - `severity` 'ERROR' | 'WARNING' | 'NOTICE', required: Rule severity
          - `short_description` string, required: Short description
          - `should_use_ai_fix` boolean, required: Whether to use AI for fixes
          - `tags` string[], required: Rule tags
          - `tests` CustomRuleRevisionTest[], required: Rule tests
            - `annotation_count` integer, required: Expected violation count
            - `code` string, required: Test code
            - `filename` string, required: Test filename
          - `tree_sitter_query` string, required: Tree-sitter query
        - `id` string, required: Revision identifier
        - `type` 'custom_rule_revision', required: Resource type
      - `name` string, required: Rule name
    - `id` string, required: Rule identifier
    - `type` 'custom_rule', required: Resource type

## Other responses

- `400`: Bad request
- `401`: Unauthorized - custom rules not enabled
- `409`: Conflict - rule already exists
- `412`: Precondition failed - validation error or ruleset not found
- `429`: Too many requests

---

[API](https://skmtc.dev/datadog/apis/datadog-api-v2-collection.md) - [All operations](https://skmtc.dev/datadog/apis/datadog-api-v2-collection/llms.txt) - [OpenAPI document](https://skmtc.dev/datadog/apis/datadog-api-v2-collection/revisions/da68bf029e4c?raw)
