## Publish Policy

### cURL

```
curl --request POST \
  --url https://your-org.platform.barndoor.ai/api/policy \
  --header 'Authorization: Bearer <token>'
```

### Python

```
import requests

url = "https://your-org.platform.barndoor.ai/api/policy"

headers = {"Authorization": "Bearer <token>"}

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

print(response.text)
```

### JavaScript (Fetch)

```
const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};

fetch('https://your-org.platform.barndoor.ai/api/policy', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

### PHP

```
<?php

$curl = curl_init();

curl_setopt_array($curl, [\
  CURLOPT_URL => "https://your-org.platform.barndoor.ai/api/policy",\
  CURLOPT_RETURNTRANSFER => true,\
  CURLOPT_ENCODING => "",\
  CURLOPT_MAXREDIRS => 10,\
  CURLOPT_TIMEOUT => 30,\
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\
  CURLOPT_CUSTOMREQUEST => "POST",\
  CURLOPT_HTTPHEADER => [\
    "Authorization: Bearer <token>"\
  ],\
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
```

### Go

```
package main

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

func main() {

url := "https://your-org.platform.barndoor.ai/api/policy"

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

req.Header.Add("Authorization", "Bearer <token>")

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

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

fmt.Println(string(body))
}
```

### Java (Unirest)

```
HttpResponse<String> response = Unirest.post("https://your-org.platform.barndoor.ai/api/policy")
  .header("Authorization", "Bearer <token>")
  .asString();
```

### Ruby

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

url = URI("https://your-org.platform.barndoor.ai/api/policy")

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

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'

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

This endpoint creates a policy in the v2 policy service.

## v2 request shape

The v2 API accepts a single policy object. It no longer uses the old `policies[].resourcePolicy` wrapper.

### Important fields:

- `name`: unique policy name within the organization
- `mcp_server_id`: target MCP server
- `application_ids`: agents/applications this policy applies to
- `status`: `DRAFT`, `ACTIVE`, `INACTIVE`, or `ARCHIVED`
- `rules`: array of rule objects using `authorized`, `actions`, `roles_groups`, and optional `condition`

### Example:

```
{
  "name": "Slack outbound policy",
  "description": "Default outbound Slack controls for the support agent.",
  "support_contact": "platform@company.com",
  "tags": ["slack", "support"],
  "status": "DRAFT",
  "mcp_server_id": "server_123",
  "application_ids": ["agent_456"],
  "rules": [\
    {\
      "name": "allow_all",\
      "authorized": true,\
      "actions": ["*"],\
      "roles_groups": ["*"]\
    },\
    {\
      "name": "block_general_channel",\
      "authorized": false,\
      "actions": ["tools/call:chat_postMessage"],\
      "roles_groups": ["*"],\
      "condition": {\
        "match": {\
          "all": {\
            "of": [\
              {\
                "expr": "request.resource.attr.channel == \"general\""\
              }\
            ]\
          }\
        }\
      }\
    }\
  ]
}
```

For a comprehensive guide, see [Manage Access Policies](https://docs.barndoor.ai/how-tos/manage-policies).

### Authorizations

Authorization

- **Type**: `string`
- **Location**: `header`
- **Required**: True
- **Description**: Bearer authentication header of the form `Bearer <token>`, where `<token>` is your auth token.

### Response

**200** - application/json

Successful Response
