> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://api-docs.botbye.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://api-docs.botbye.com/_mcp/server.

# create

POST {base_url}/api/v1/{account_id}/projects/{project_id}/metrics
Content-Type: application/json

Create a new metric definition.

Reference: https://api-docs.botbye.com/bot-bye-api/protection/metric-definitions/create

## OpenAPI Specification

```yaml
openapi: 3.1.0
info:
  title: collection
  version: 1.0.0
paths:
  /api/v1/{account_id}/projects/{project_id}/metrics:
    post:
      operationId: create
      summary: create
      description: Create a new metric definition.
      tags:
        - subpackage_metricDefinitions
      parameters:
        - name: account_id
          in: path
          required: true
          schema:
            type: string
        - name: project_id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Successful response
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                aggregation:
                  $ref: >-
                    #/components/schemas/ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaAggregation
                field:
                  type:
                    - string
                    - 'null'
                filter:
                  oneOf:
                    - $ref: >-
                        #/components/schemas/ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilter
                    - type: 'null'
                timeWindow:
                  type: integer
                  description: Time window in seconds.
                keyBy:
                  $ref: >-
                    #/components/schemas/ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaKeyBy
                enabled:
                  type: boolean
              required:
                - name
                - aggregation
                - timeWindow
                - keyBy
servers:
  - url: '{base_url}'
    description: '{base_url}'
components:
  schemas:
    ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaAggregation:
      type: string
      enum:
        - COUNT
        - DISTINCT_COUNT
        - RATE
        - LAST_VALUE
        - SUM
      title: >-
        ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaAggregation
    ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilterConditionsItemsOperator:
      type: string
      enum:
        - EQ
        - NEQ
        - IN
        - GT
        - LT
        - GTE
        - LTE
      title: >-
        ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilterConditionsItemsOperator
    ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilterConditionsItems:
      type: object
      properties:
        field:
          type: string
        operator:
          $ref: >-
            #/components/schemas/ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilterConditionsItemsOperator
        value:
          type: string
      required:
        - field
        - operator
        - value
      title: >-
        ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilterConditionsItems
    ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilter:
      type: object
      properties:
        conditions:
          type: array
          items:
            $ref: >-
              #/components/schemas/ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilterConditionsItems
      required:
        - conditions
      title: >-
        ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaFilter
    ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaKeyBy:
      type: string
      enum:
        - ACCOUNT
        - IP
        - DEVICE
        - DEVICE_IP
      title: >-
        ApiV1AccountIdProjectsProjectIdMetricsPostRequestBodyContentApplicationJsonSchemaKeyBy

```

## Examples



**Request**

```json
{
  "name": "Login Count",
  "aggregation": "COUNT",
  "timeWindow": 3600,
  "keyBy": "ACCOUNT",
  "field": "eventType",
  "filter": {
    "conditions": [
      {
        "field": "eventType",
        "operator": "EQ",
        "value": "login"
      }
    ]
  },
  "enabled": true
}
```

**SDK Code**

```python
import requests

url = "https://{base_url}/api/v1/account_id/projects/project_id/metrics"

payload = {
    "name": "Login Count",
    "aggregation": "COUNT",
    "timeWindow": 3600,
    "keyBy": "ACCOUNT",
    "field": "eventType",
    "filter": { "conditions": [
            {
                "field": "eventType",
                "operator": "EQ",
                "value": "login"
            }
        ] },
    "enabled": True
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript
const url = 'https://{base_url}/api/v1/account_id/projects/project_id/metrics';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"name":"Login Count","aggregation":"COUNT","timeWindow":3600,"keyBy":"ACCOUNT","field":"eventType","filter":{"conditions":[{"field":"eventType","operator":"EQ","value":"login"}]},"enabled":true}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://{base_url}/api/v1/account_id/projects/project_id/metrics"

	payload := strings.NewReader("{\n  \"name\": \"Login Count\",\n  \"aggregation\": \"COUNT\",\n  \"timeWindow\": 3600,\n  \"keyBy\": \"ACCOUNT\",\n  \"field\": \"eventType\",\n  \"filter\": {\n    \"conditions\": [\n      {\n        \"field\": \"eventType\",\n        \"operator\": \"EQ\",\n        \"value\": \"login\"\n      }\n    ]\n  },\n  \"enabled\": true\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://{base_url}/api/v1/account_id/projects/project_id/metrics")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"name\": \"Login Count\",\n  \"aggregation\": \"COUNT\",\n  \"timeWindow\": 3600,\n  \"keyBy\": \"ACCOUNT\",\n  \"field\": \"eventType\",\n  \"filter\": {\n    \"conditions\": [\n      {\n        \"field\": \"eventType\",\n        \"operator\": \"EQ\",\n        \"value\": \"login\"\n      }\n    ]\n  },\n  \"enabled\": true\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://{base_url}/api/v1/account_id/projects/project_id/metrics")
  .header("Content-Type", "application/json")
  .body("{\n  \"name\": \"Login Count\",\n  \"aggregation\": \"COUNT\",\n  \"timeWindow\": 3600,\n  \"keyBy\": \"ACCOUNT\",\n  \"field\": \"eventType\",\n  \"filter\": {\n    \"conditions\": [\n      {\n        \"field\": \"eventType\",\n        \"operator\": \"EQ\",\n        \"value\": \"login\"\n      }\n    ]\n  },\n  \"enabled\": true\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://{base_url}/api/v1/account_id/projects/project_id/metrics', [
  'body' => '{
  "name": "Login Count",
  "aggregation": "COUNT",
  "timeWindow": 3600,
  "keyBy": "ACCOUNT",
  "field": "eventType",
  "filter": {
    "conditions": [
      {
        "field": "eventType",
        "operator": "EQ",
        "value": "login"
      }
    ]
  },
  "enabled": true
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://{base_url}/api/v1/account_id/projects/project_id/metrics");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"name\": \"Login Count\",\n  \"aggregation\": \"COUNT\",\n  \"timeWindow\": 3600,\n  \"keyBy\": \"ACCOUNT\",\n  \"field\": \"eventType\",\n  \"filter\": {\n    \"conditions\": [\n      {\n        \"field\": \"eventType\",\n        \"operator\": \"EQ\",\n        \"value\": \"login\"\n      }\n    ]\n  },\n  \"enabled\": true\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "name": "Login Count",
  "aggregation": "COUNT",
  "timeWindow": 3600,
  "keyBy": "ACCOUNT",
  "field": "eventType",
  "filter": ["conditions": [
      [
        "field": "eventType",
        "operator": "EQ",
        "value": "login"
      ]
    ]],
  "enabled": true
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://{base_url}/api/v1/account_id/projects/project_id/metrics")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```