> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirage.strukto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Create policy

> Save a named Python policy program. Returns `409` if the name already exists.

> [accordion] Errors
>
> | Status | Meaning |
> | - | - |
> | `400` | Bad request: malformed, a host not allowed, or a browser page from another origin |
> | `401` | The token is missing or wrong |
> | `409` | The id or name is taken, the workspace is busy, or a file changed since it was read |
> | `422` | Validation Error |
> | `429` | Past a rate limit, or a new session past max\_sessions |
> | `501` | The server requires accounts; saved configs are kept only on a server of one account |
>
> See [error examples and recovery steps](https://docs.mirage.strukto.ai/api/errors.md). The [complete OpenAPI contract](https://github.com/strukto-ai/mirage/blob/main/docs/openapi.yaml) includes every response schema.


## OpenAPI

````yaml /openapi.yaml post /v1/policies
openapi: 3.2.0
info:
  title: Mirage API
  version: '0.1'
servers:
  - url: http://127.0.0.1:8765
    description: A local server on its default port
security:
  - bearerAuth: []
tags:
  - name: Workspace operations
    x-group: Workspaces
  - name: Workspaces
    x-group: Lifecycle
    parent: Workspace operations
  - name: Sessions
  - name: Shell
    parent: Workspace operations
  - name: VFS
    parent: Workspace operations
  - name: Tools
  - name: Documents
    x-group: Agent Instructions
    parent: Workspace operations
  - name: Snapshots
  - name: Templates
    parent: Configuration
  - name: Saved VFS
    x-group: VFS
    parent: Configuration
  - name: Saved CLIs
    x-group: CLI
    parent: Configuration
  - name: Saved runtimes
    x-group: Runtime
    parent: Configuration
  - name: Saved machines
    x-group: Machine
    parent: Configuration
  - name: Configuration
  - name: Policies
    parent: Configuration
  - name: Asks
    parent: Workspace operations
    x-group: Approval requests
  - name: Lines
    parent: Workspace operations
    x-group: Commands
  - name: Jobs
    parent: Workspace operations
  - name: Processes
    parent: Workspace operations
  - name: Machines
    parent: Workspace operations
  - name: Server
  - name: Service
    parent: Server
  - name: Daemon
    parent: Server
  - name: Auth
paths:
  /v1/policies:
    post:
      tags:
        - Policies
      summary: Create policy
      description: >-
        Save a named Python policy program. Returns `409` if the name already
        exists.
      operationId: create_policy_v1_policies_post
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreatePolicyRequest'
            example:
              name: guard.py
              source: |
                def pre_command(ctx):
                    if ctx["command"] == "rm":
                        return {"kind": "deny", "reason": "Deletes are disabled"}
        required: true
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PolicyResponse'
components:
  schemas:
    CreatePolicyRequest:
      additionalProperties: false
      properties:
        name:
          type: string
          description: Ends in .py, such as guard.py.
        source:
          type: string
          description: Python policy source code, up to 64 KiB.
      required:
        - name
        - source
      type: object
    PolicyResponse:
      properties:
        name:
          type: string
        source:
          type: string
      required:
        - name
        - source
      type: object
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        The server's token (MIRAGE_AUTH_TOKEN) or a JWT. A daemon in the default
        local mode with no token accepts requests without one.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.