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

# Post policy

> Updates the policy in Oso Cloud. The policy should be represented as a string containing [Polar](https://www.osohq.com/docs/modeling-in-polar/reference) code.



## OpenAPI

````yaml post /policy
openapi: 3.1.0
info:
  title: Oso Cloud HTTP API
  version: 0.1.0
  description: >-
    <p>Oso Cloud exposes an HTTP API that you can use to make queries directly,
    without using one of the clients.</p><p>For endpoints that require
    authentication, pass your API key as an HTTP Bearer Auth payload.</p><p>For
    example, using curl: <code>curl -H &quot;Authorization: Bearer
    $OSO_AUTH&quot; https://cloud.osohq.com/api/</code></p>
servers:
  - url: https://api.osohq.com/api/
security: []
paths:
  /policy:
    post:
      tags:
        - Policy
      description: >-
        Updates the policy in Oso Cloud. The policy should be represented as a
        string containing
        [Polar](https://www.osohq.com/docs/modeling-in-polar/reference) code.
      operationId: post_policy
      parameters:
        - name: force
          in: query
          required: true
          schema:
            type: boolean
        - name: show_suggestions
          in: query
          required: true
          schema:
            type: boolean
        - name: fail_fast
          in: query
          required: true
          schema:
            type: boolean
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Policy'
        required: true
      responses:
        '200':
          description: The policy was updated.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiResult'
        '202':
          description: The policy was not updated because it is unchanged.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiResult'
        default:
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SavePolicyError'
      security:
        - ApiKey: []
      x-codeSamples:
        - lang: javascript
          label: Node.js
          source: |
            import { Oso } from 'oso-cloud';

            const apiKey = process.env.OSO_CLOUD_API_KEY;
            const oso = new Oso("https://cloud.osohq.com", apiKey);

            // Update policy with Polar code
            const policyCode = `
              actor User {}
              resource Repository {
                permissions = ["read", "write"];
                roles = ["owner", "maintainer"];
              }
              has_role(user: User, "owner", repo: Repository) if
                user.id = repo.owner_id;
            `;
            await oso.policy(policyCode);
        - lang: python
          label: Python
          source: |
            import os
            from oso_cloud import Oso

            oso = Oso(api_key=os.environ.get('OSO_CLOUD_API_KEY', None))

            # Update policy with Polar code
            policy_code = """
            actor User {}
            resource Repository {
                permissions = ["read", "write"];
                roles = ["owner", "maintainer"];
            }
            has_role(user: User, "owner", repo: Repository) if
                user.id = repo.owner_id;
            """
            oso.policy(policy_code)
        - lang: go
          label: Go
          source: |
            package main

            import (
                "log"
                "os"
                oso "github.com/osohq/go-oso-cloud/v2"
            )

            func main() {
                apiKey := os.Getenv("OSO_CLOUD_API_KEY")
                osoClient := oso.NewClient("https://cloud.osohq.com", apiKey)

            // Update policy with Polar code
            policyCode := `
            actor User {}
            resource Repository {
                permissions = ["read", "write"];
                roles = ["owner", "maintainer"];
            }
            has_role(user: User, "owner", repo: Repository) if
                user.id = repo.owner_id;
            `
                err := osoClient.Policy(policyCode)
                if err != nil {
                    log.Fatal(err)
                }
            }
        - lang: java
          label: Java
          source: |
            package com.mycompany;

            import java.io.IOException;
            import com.osohq.oso_cloud.Oso;
            import com.osohq.oso_cloud.api.ApiException;

            public class App {
                public static void main(String[] args) {
                    String apiKey = System.getenv("OSO_CLOUD_API_KEY");
                    Oso oso = new Oso(apiKey);

            // Update policy with Polar code
            String policyCode = """
                actor User {}
                resource Repository {
                    permissions = ["read", "write"];
                    roles = ["owner", "maintainer"];
                }
                has_role(user: User, "owner", repo: Repository) if
                    user.id = repo.owner_id;
                """;
                    try {
                        oso.policy(policyCode);
                    } catch (IOException | ApiException e) {
                        System.err.println("Error updating policy: " + e.getMessage());
                    }
                }
            }
        - lang: ruby
          label: Ruby
          source: >
            require 'oso-cloud'


            api_key = ENV.fetch('OSO_CLOUD_API_KEY', nil)

            oso = OsoCloud::Oso.new(url: "https://cloud.osohq.com", api_key:
            api_key)


            # Update policy with Polar code

            policy_code = <<~POLAR
              actor User {}
              resource Repository {
                permissions = ["read", "write"];
                roles = ["owner", "maintainer"];
              }
              has_role(user: User, "owner", repo: Repository) if
                user.id = repo.owner_id;
            POLAR

            oso.policy(policy_code)
        - lang: csharp
          label: C#
          source: >
            using OsoCloud;


            string? apiKey =
            Environment.GetEnvironmentVariable("OSO_CLOUD_API_KEY");

            var oso = new Oso("https://api.osohq.com", apiKey);


            // Update policy with Polar code

            var policyCode = @"
                actor User {}
                resource Repository {
                    permissions = [""read"", ""write""];
                    roles = [""owner"", ""maintainer""];
                }
                has_role(user: User, ""owner"", repo: Repository) if
                    user.id = repo.owner_id;
            ";

            await oso.Policy(policyCode);
components:
  schemas:
    Policy:
      type: object
      required:
        - src
      properties:
        filename:
          type: string
          nullable: true
        src:
          type: string
    ApiResult:
      type: object
      required:
        - message
      properties:
        message:
          type: string
    SavePolicyError:
      oneOf:
        - type: object
          required:
            - error_type
            - message
          properties:
            error_type:
              type: string
              enum:
                - Generic
            message:
              type: string
        - type: object
          required:
            - error_type
            - errors
            - message
          properties:
            error_type:
              type: string
              enum:
                - Validation
            message:
              type: string
            errors:
              type: array
              items:
                $ref: '#/components/schemas/PolicyError'
        - type: object
          required:
            - error_type
            - message
            - test_results
          properties:
            error_type:
              type: string
              enum:
                - TestsFailed
            message:
              type: string
            test_results:
              $ref: '#/components/schemas/PolicyTestResult'
    PolicyError:
      description: >-
        Error report for a failed Polar policy test, to be displayed to user.
        Sent to `oso-cloud` CLI as JSON.
      type: object
      required:
        - error_type
        - message
      properties:
        error_type:
          description: Type of error encountered.
          allOf:
            - $ref: '#/components/schemas/PolicyFailure'
        message:
          description: Error message to display to the user.
          type: string
    PolicyTestResult:
      description: Result of a Policy test API request. Sent to `oso-cloud` CLI as JSON.
      type: object
      required:
        - errors
        - success
        - tests
      properties:
        success:
          description: Did the policy test succeed?
          type: boolean
        errors:
          description: What errors did we encounter?
          type: array
          items:
            $ref: '#/components/schemas/PolicyError'
        tests:
          description: What tests did we execute?
          type: array
          items:
            $ref: '#/components/schemas/TestSummary'
    PolicyFailure:
      description: >-
        All known failure modes for a submitted policy test. Encountering any of
        these scenarios means the policy test has failed.
      oneOf:
        - description: Polar file failed validation.
          type: string
          enum:
            - ValidationFailed
        - description: An assertion failed in an executed test.
          type: string
          enum:
            - AssertionFailed
        - description: Server hit an unexpected error.
          type: string
          enum:
            - ServerError
    TestSummary:
      description: Results of executing a single test. Sent to `oso-cloud` CLI as JSON.
      type: object
      required:
        - name
        - passed
        - total
      properties:
        name:
          description: Name of test.
          type: string
        passed:
          description: How many assertions passed?
          type: integer
          format: uint
          minimum: 0
        total:
          description: How many assertions in total?
          type: integer
          format: uint
          minimum: 0
  securitySchemes:
    ApiKey:
      description: Requires an API key to access.
      type: http
      scheme: bearer
      bearerFormat: Bearer e_0123_123_token0123

````