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

# Attach a policy

> Applies a policy to every request, or to the requests for the models named. It applies from the next request on.



## OpenAPI

````yaml /openapi/source/external/agent-gateway/agent-gateway.json post /agent_gateways/{instance_id}/policy_attachments
openapi: 3.1.0
info:
  version: 1.0.0
  title: Telnyx API
  x-latency-category: interactive
  x-endpoint-cost: light
  description: >-
    Agent Gateway authenticates, authorizes, and audits the requests agents send
    to models, tools, and other agents. Each gateway runs as a dedicated
    instance on the account's
    [*Network*](https://developers.telnyx.com/docs/network/overview). This API
    creates, lists, and deletes instances, and manages the keys, teams, spend
    logs, MCP servers, APIs, tool permissions, and guardrails on them. Each
    instance serves an OpenAI-compatible API at its `gateway_url`.
servers:
  - url: https://api.telnyx.com/v2
    description: Telnyx API v2
security:
  - bearerAuth: []
tags:
  - name: Agent Gateways
    description: Create, list, retrieve, and delete Agent Gateway instances.
  - name: Agent Gateway Keys
    description: Keys on an instance.
  - name: Agent Gateway Teams
    description: 'Teams on an instance: shared model lists, budgets, and rate limits.'
  - name: Agent Gateway Spend Logs
    description: The requests an instance served, with their cost.
  - name: Agent Gateway MCP Servers
    description: MCP servers on an instance, served to agents at one MCP endpoint.
  - name: Agent Gateway APIs
    description: REST APIs on an instance, served to agents as tools.
  - name: Agent Gateway Tool Permissions
    description: The MCP servers and tools a key or team may use.
  - name: Agent Gateway Guardrails
    description: Checks an instance runs on model and tool requests.
  - name: Agent Gateway Policies
    description: Named sets of guardrails an instance applies to requests.
  - name: Agent Gateway Policy Attachments
    description: Where an instance applies each policy.
paths:
  /agent_gateways/{instance_id}/policy_attachments:
    post:
      tags:
        - Agent Gateway Policy Attachments
      summary: Attach a policy
      description: >-
        Applies a policy to every request, or to the requests for the models
        named. It applies from the next request on.
      operationId: CreateAgentGatewayPolicyAttachment
      parameters:
        - name: instance_id
          in: path
          required: true
          description: The Agent Gateway's `id`.
          schema:
            type: string
            format: uuid
          example: 32ce035d-3165-4031-b1a7-a7c0c259590a
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AgentGatewayPolicyAttachmentCreate'
            examples:
              models:
                summary: Some models, with a priority
                value:
                  policy: strict
                  models:
                    - GLM-5.3*
                  priority: 10
              default:
                summary: The default
                value:
                  policy: baseline
                  default: true
      responses:
        '201':
          description: The attachment
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AgentGatewayPolicyAttachmentResponse'
              example:
                data:
                  id: 3394712f-2ad5-428a-8a3d-7a08d4b2906d
                  policy: strict
                  models:
                    - GLM-5.3*
                  priority: 10
                  default: false
                  created_at: '2026-11-02T12:58:55.388000Z'
                  updated_at: '2026-11-02T12:58:55.388000Z'
        '400':
          description: A policy the instance does not have
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TelnyxErrors'
              example:
                errors:
                  - code: '10013'
                    title: Missing association
                    detail: One of the associated fields does not exist.
                    meta:
                      url: https://developers.telnyx.com/docs/overview/errors/10013
                    source:
                      pointer: /policy
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TelnyxErrors'
              example:
                errors:
                  - code: '10009'
                    title: Authentication failed
                    detail: >-
                      The required authentication headers were either invalid or
                      not included in the request.
                    meta:
                      url: https://developers.telnyx.com/docs/overview/errors/10009
        '404':
          description: Not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TelnyxErrors'
              example:
                errors:
                  - code: '10005'
                    title: Resource not found
                    detail: The requested resource or URL could not be found.
                    meta:
                      url: https://developers.telnyx.com/docs/overview/errors/10005
      x-codeSamples:
        - lang: JavaScript
          source: >-
            import Telnyx from 'telnyx';


            const client = new Telnyx({
              apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
            });


            const AgentGatewayPolicyAttachment = await
            client.agentGateways.policyAttachments.create('instance_id', {
              policy: 'policy',
            });


            console.log(AgentGatewayPolicyAttachment.data);
        - lang: Python
          source: >
            import os

            from telnyx import Telnyx


            client = Telnyx(
                api_key=os.environ.get("TELNYX_API_KEY"),  # This is the default and can be omitted
            )

            agent_gateway_policy_attachment =
            client.agent_gateways.policy_attachments.create(
                instance_id="instance_id",
                policy="policy",
            )

            print(agent_gateway_policy_attachment.data)
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/team-telnyx/telnyx-go\"\n\t\"github.com/team-telnyx/telnyx-go/option\"\n)\n\nfunc main() {\n\tclient := telnyx.NewClient(\n\t\toption.WithAPIKey(\"My API Key\"),\n\t)\n\tpolicyAttachment, err := client.AgentGateways.PolicyAttachments.New(\n\t\tcontext.TODO(),\n\t\t\"instance_id\",\n\t\ttelnyx.AgentGatewayPolicyAttachmentNewParams{\n\t\t\tPolicy: \"policy\",\n\t\t},\n\t)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfmt.Printf(\"%+v\\n\", policyAttachment.Data)\n}\n"
        - lang: Java
          source: >-
            package com.telnyx.sdk.example;


            import com.telnyx.sdk.client.TelnyxClient;

            import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;

            import
            com.telnyx.sdk.models.agentGateways.policyAttachments.PolicyAttachmentCreateParams;


            public final class Main {
                private Main() {}

                public static void main(String[] args) {
                    TelnyxClient client = TelnyxOkHttpClient.fromEnv();

                    PolicyAttachmentCreateParams params = PolicyAttachmentCreateParams.builder()
                        .policy("policy")
                        .build();
                    var response = client.agentGateways().policyAttachments().create("instance_id", params);
                }
            }
        - lang: Ruby
          source: >-
            require "telnyx"


            telnyx = Telnyx::Client.new(api_key: "My API Key")


            agent_gateway_policy_attachment =
            telnyx.agent_gateways.policy_attachments.create("instance_id",
            policy: "policy")


            puts(agent_gateway_policy_attachment)
        - lang: PHP
          source: >-
            <?php


            require_once dirname(__DIR__) . '/vendor/autoload.php';


            use Telnyx\Client;

            use Telnyx\Core\Exceptions\APIException;


            $client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API
            Key');


            try {
              $agent_gateway_policy_attachment = $client->agentGateways->policyAttachments->create(
                'instance_id',
                policy: 'policy',
              );

              var_dump($agent_gateway_policy_attachment);
            } catch (APIException $e) {
              echo $e->getMessage();
            }
        - lang: CLI
          source: |-
            telnyx agent-gateways:policy-attachments create \
              --api-key 'My API Key' \
              --instance-id 32ce035d-3165-4031-b1a7-a7c0c259590a \
              --policy policy
components:
  schemas:
    AgentGatewayPolicyAttachmentCreate:
      type: object
      required:
        - policy
      properties:
        policy:
          type: string
          description: The policy to apply, by name.
        models:
          type: array
          items:
            type: string
          default: []
          description: >-
            The models whose requests the policy applies to, by name. A name can
            use `*` as a wildcard. Empty applies the policy to every request.
        priority:
          anyOf:
            - type: integer
            - type: 'null'
          description: An attachment with a priority applies before the others.
        default:
          type: boolean
          default: false
          description: >-
            `true` applies the policy only to the requests no other attachment
            matches.
    AgentGatewayPolicyAttachmentResponse:
      type: object
      required:
        - data
      properties:
        data:
          $ref: '#/components/schemas/AgentGatewayPolicyAttachment'
    TelnyxErrors:
      type: object
      properties:
        errors:
          type: array
          items:
            $ref: '#/components/schemas/TelnyxError'
    AgentGatewayPolicyAttachment:
      type: object
      description: Where an instance applies a policy.
      required:
        - id
        - policy
        - models
        - priority
        - default
        - created_at
        - updated_at
      properties:
        id:
          type: string
          format: uuid
          description: The attachment's ID.
        policy:
          type: string
          description: The policy to apply, by name.
        models:
          type: array
          items:
            type: string
          default: []
          description: >-
            The models whose requests the policy applies to, by name. A name can
            use `*` as a wildcard. Empty applies the policy to every request.
        priority:
          anyOf:
            - type: integer
            - type: 'null'
          description: An attachment with a priority applies before the others.
        default:
          type: boolean
          default: false
          description: >-
            `true` applies the policy only to the requests no other attachment
            matches.
        created_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          description: When the attachment was created.
        updated_at:
          anyOf:
            - type: string
              format: date-time
            - type: 'null'
          description: When the attachment last changed.
    TelnyxError:
      type: object
      properties:
        code:
          type: string
        title:
          type: string
        detail:
          type: string
        source:
          type: object
          properties:
            pointer:
              type: string
            parameter:
              type: string
        meta:
          type: object
          properties:
            url:
              type: string
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Telnyx API key

````