{
  "openapi": "3.0.0",
  "info": {
    "title": "YGO API: Save text template",
    "version": "2.0.0"
  },
  "servers": [
    {
      "url": "https://api.ygo.ai",
      "description": "Production and Sandbox (determined by API key)"
    }
  ],
  "paths": {
    "/v2/project/content/saveTextTemplate": {
      "post": {
        "summary": "Save text template",
        "description": "Create a new text template or update an existing one. The template is matched within the project by its TemplateKey: an existing key updates that template, a new key creates one. The saved template is returned.\n\nTemplateKey, EntityType, and Instruction are required. The default Instruction and each per-locale instruction are checked for safety before the template is stored.",
        "operationId": "saveTextTemplate",
        "tags": [
          "Content Enrichment API/Templates"
        ],
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/X-Project-ID"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SaveTextTemplateRequest"
              },
              "examples": {
                "createNew": {
                  "summary": "Create a new hotel summary template",
                  "value": {
                    "TemplateKey": "summary",
                    "Name": "Hotel Summary",
                    "EntityType": "hotel",
                    "Description": "Short marketing summary shown at the top of the hotel page.",
                    "Instruction": "Write a concise, factual two-sentence summary highlighting the hotel's location, standout amenity, and overall character. Avoid superlatives.",
                    "LocaleInstructions": {
                      "de-DE": "Schreibe eine sachliche Zusammenfassung in zwei Saetzen und verwende die Sie-Form."
                    },
                    "Status": "active",
                    "RenderLayout": "prose"
                  }
                },
                "updateExisting": {
                  "summary": "Update the room description template's instruction",
                  "value": {
                    "TemplateKey": "room_description",
                    "Name": "Room Description",
                    "EntityType": "room",
                    "Description": "Per-room descriptive paragraph used on room cards.",
                    "Instruction": "Describe the room in one short paragraph: size, bedding, view, and the most notable in-room features. Mention any balcony or terrace. Keep it neutral and concrete.",
                    "LocaleInstructions": {},
                    "Status": "active",
                    "RenderLayout": "roomcards"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Text template created or updated successfully. Returns the saved template.",
            "headers": {
              "X-Trace-ID": {
                "$ref": "#/components/headers/X-Trace-ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SaveTextTemplateResponse"
                },
                "examples": {
                  "createNew": {
                    "summary": "Newly created hotel summary template",
                    "value": {
                      "Data": {
                        "ID": "0192fb40-1aa2-7def-9abc-1122334455aa",
                        "ProjectID": "0192fb00-0000-7000-8000-000000000001",
                        "TemplateKey": "summary",
                        "Name": "Hotel Summary",
                        "EntityType": "hotel",
                        "Description": "Short marketing summary shown at the top of the hotel page.",
                        "Instruction": "Write a concise, factual two-sentence summary highlighting the hotel's location, standout amenity, and overall character. Avoid superlatives.",
                        "LocaleInstructions": {
                          "de-DE": "Schreibe eine sachliche Zusammenfassung in zwei Saetzen und verwende die Sie-Form."
                        },
                        "Status": "active",
                        "RenderLayout": "prose",
                        "CreatedAt": "2026-04-12T09:15:00Z",
                        "UpdatedAt": "2026-04-12T09:15:00Z"
                      }
                    }
                  },
                  "updateExisting": {
                    "summary": "Updated room description template",
                    "value": {
                      "Data": {
                        "ID": "0192fb40-2bb3-7def-9abc-2233445566bb",
                        "ProjectID": "0192fb00-0000-7000-8000-000000000001",
                        "TemplateKey": "room_description",
                        "Name": "Room Description",
                        "EntityType": "room",
                        "Description": "Per-room descriptive paragraph used on room cards.",
                        "Instruction": "Describe the room in one short paragraph: size, bedding, view, and the most notable in-room features. Mention any balcony or terrace. Keep it neutral and concrete.",
                        "LocaleInstructions": {},
                        "Status": "active",
                        "RenderLayout": "roomcards",
                        "CreatedAt": "2026-03-01T08:00:00Z",
                        "UpdatedAt": "2026-04-12T09:15:00Z"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "429": {
            "$ref": "#/components/responses/TooManyRequests"
          },
          "500": {
            "$ref": "#/components/responses/InternalServerError"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ErrorDetail": {
        "type": "object",
        "required": [
          "Code",
          "Message"
        ],
        "description": "Error detail containing a machine-readable code and human-readable message.",
        "properties": {
          "Code": {
            "type": "string",
            "description": "Machine-readable error code for programmatic handling.",
            "example": "VALIDATION_ERROR"
          },
          "Message": {
            "type": "string",
            "description": "Human-readable error message.",
            "example": "Request validation failed"
          },
          "Fields": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/FieldError"
            },
            "description": "Field-level validation errors. Only present for VALIDATION_ERROR responses."
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": [
          "Error"
        ],
        "description": "Standard v2 error response wrapper.",
        "properties": {
          "Error": {
            "$ref": "#/components/schemas/ErrorDetail"
          }
        }
      },
      "FieldError": {
        "type": "object",
        "required": [
          "Field",
          "Message"
        ],
        "description": "A single field-level validation error.",
        "properties": {
          "Field": {
            "type": "string",
            "description": "Name of the field that failed validation.",
            "example": "TemplateKey"
          },
          "Message": {
            "type": "string",
            "description": "Human-readable validation error message for this field.",
            "example": "TemplateKey is required"
          },
          "Code": {
            "type": "string",
            "enum": [
              "REQUIRED",
              "TOO_LONG",
              "TOO_SHORT",
              "OUT_OF_RANGE",
              "TOO_MANY",
              "INVALID_FORMAT",
              "NOT_ALLOWED",
              "CONFLICT",
              "INVALID_ENCODING",
              "INSECURE_SCHEME",
              "UNSAFE_CONTENT",
              "REQUIRED_WHEN_ENABLED"
            ],
            "description": "Machine-readable reason the field failed validation. Optional; clients should fall back to Message when it is absent or unrecognized.",
            "example": "TOO_LONG"
          },
          "Params": {
            "type": "object",
            "additionalProperties": true,
            "description": "Machine-readable values the message is built from, such as limits or allowed values. Keys are PascalCase; which keys are present depends on Code. Optional.",
            "example": {
              "Max": 100
            }
          }
        }
      },
      "SaveTextTemplateRequest": {
        "type": "object",
        "required": [
          "TemplateKey",
          "EntityType",
          "Instruction"
        ],
        "description": "Request body for creating or updating a text template. The template is matched within the project by TemplateKey.",
        "properties": {
          "TemplateKey": {
            "type": "string",
            "maxLength": 100,
            "description": "Stable key identifying the template within the project (max 100 characters). The generated text for this template is stored under this key (e.g. 'summary', 'room_description'). A key may be namespaced with a leading 'group.' prefix to define a variant within a template group. Saving with an existing key updates that template; a new key creates one.",
            "example": "summary"
          },
          "Name": {
            "type": "string",
            "description": "Human-readable template name for display. Optional.",
            "example": "Hotel Summary"
          },
          "EntityType": {
            "type": "string",
            "enum": [
              "hotel",
              "room"
            ],
            "description": "Whether the template produces a hotel-level text or a room-level text.",
            "example": "hotel"
          },
          "Description": {
            "type": "string",
            "description": "Free-text note describing what the template is for. Optional.",
            "example": "Short marketing summary shown at the top of the hotel page."
          },
          "Instruction": {
            "type": "string",
            "maxLength": 12000,
            "description": "Default authoring instruction that guides text generation when no locale-specific override applies. Required. Limited to 12,000 characters (Unicode characters, not bytes); longer instructions are rejected.",
            "example": "Write a concise, factual two-sentence summary highlighting the hotel's location, standout amenity, and overall character. Avoid superlatives."
          },
          "LocaleInstructions": {
            "type": "object",
            "maxProperties": 100,
            "additionalProperties": {
              "type": "string",
              "maxLength": 2500
            },
            "description": "Per-locale authoring instructions keyed by BCP 47 language tag (e.g. 'de-DE'). When a locale has an entry here, it overrides the default Instruction for that locale; otherwise the default Instruction is used. Optional; omit or pass an empty object for no overrides. Each per-locale instruction is limited to 2,500 characters, counted independently of the base instruction, and a template carries at most 100 locale entries.",
            "example": {
              "de-DE": "Schreibe eine sachliche Zusammenfassung in zwei Saetzen und verwende die Sie-Form."
            }
          },
          "Status": {
            "type": "string",
            "enum": [
              "active",
              "inactive"
            ],
            "description": "Lifecycle status. Only 'active' templates are used during text generation. Defaults to 'active' when omitted.",
            "example": "active"
          },
          "RenderLayout": {
            "type": "string",
            "enum": [
              "prose",
              "highlights",
              "roomcards",
              "summary"
            ],
            "description": "Optional rendering hint suggesting how a client should display this template's generated text. Leave empty or omit when no hint is needed.",
            "example": "prose"
          }
        }
      },
      "SaveTextTemplateResponse": {
        "type": "object",
        "required": [
          "Data"
        ],
        "description": "Single-object response envelope wrapping the saved text template.",
        "properties": {
          "Data": {
            "$ref": "#/components/schemas/TextTemplate"
          }
        }
      },
      "TextTemplate": {
        "type": "object",
        "required": [
          "ID",
          "ProjectID",
          "TemplateKey",
          "EntityType",
          "Instruction",
          "LocaleInstructions",
          "Status",
          "CreatedAt",
          "UpdatedAt"
        ],
        "description": "A project-scoped text template. It controls one generated text per locale for either a hotel or a room, using a default authoring instruction plus optional per-locale instruction overrides.",
        "properties": {
          "ID": {
            "type": "string",
            "format": "uuid",
            "description": "Unique template UUID.",
            "example": "0192fb40-1aa2-7def-9abc-1122334455aa"
          },
          "ProjectID": {
            "type": "string",
            "format": "uuid",
            "description": "UUID of the project that owns this template.",
            "example": "0192fb00-0000-7000-8000-000000000001"
          },
          "TemplateKey": {
            "type": "string",
            "description": "Stable key identifying the template within the project. The generated text for this template is stored under this key (e.g. 'summary', 'room_description'). A key may be namespaced with a leading 'group.' prefix to define a variant within a template group.",
            "example": "summary"
          },
          "Name": {
            "type": "string",
            "description": "Human-readable template name for display. May be empty.",
            "example": "Hotel Summary"
          },
          "EntityType": {
            "type": "string",
            "enum": [
              "hotel",
              "room"
            ],
            "description": "Whether the template produces a hotel-level text or a room-level text.",
            "example": "hotel"
          },
          "Description": {
            "type": "string",
            "description": "Free-text note describing what the template is for. May be empty.",
            "example": "Short marketing summary shown at the top of the hotel page."
          },
          "Instruction": {
            "type": "string",
            "description": "Default authoring instruction that guides text generation when no locale-specific override applies.",
            "example": "Write a concise, factual two-sentence summary highlighting the hotel's location, standout amenity, and overall character. Avoid superlatives."
          },
          "LocaleInstructions": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            },
            "description": "Per-locale authoring instructions keyed by BCP 47 language tag (e.g. 'de-DE'). When a locale has an entry here, it overrides the default Instruction for that locale; otherwise the default Instruction is used. An empty object means no per-locale overrides.",
            "example": {
              "de-DE": "Schreibe eine sachliche Zusammenfassung in zwei Saetzen und verwende die Sie-Form."
            }
          },
          "Status": {
            "type": "string",
            "enum": [
              "active",
              "inactive"
            ],
            "description": "Lifecycle status. Only 'active' templates are used during text generation.",
            "example": "active"
          },
          "RenderLayout": {
            "type": "string",
            "enum": [
              "prose",
              "highlights",
              "roomcards",
              "summary"
            ],
            "description": "Optional rendering hint suggesting how a client should display this template's generated text. Empty when no hint is set.",
            "example": "prose"
          },
          "CreatedAt": {
            "type": "string",
            "format": "date-time",
            "description": "Timestamp when the template was created (RFC3339).",
            "example": "2026-03-01T08:00:00Z"
          },
          "UpdatedAt": {
            "type": "string",
            "format": "date-time",
            "description": "Timestamp when the template was last updated (RFC3339).",
            "example": "2026-04-12T09:15:00Z"
          }
        }
      }
    },
    "parameters": {
      "X-Project-ID": {
        "name": "X-Project-ID",
        "in": "header",
        "required": true,
        "description": "Project UUID. Required for all project-scoped endpoints.",
        "example": "$YGO_PROJECT_ID",
        "schema": {
          "type": "string",
          "format": "uuid"
        }
      }
    },
    "headers": {
      "Retry-After": {
        "description": "Number of seconds to wait before retrying the request. Present on 429 responses.",
        "schema": {
          "type": "integer",
          "minimum": 1,
          "example": 30
        }
      },
      "X-Trace-ID": {
        "description": "Request trace ID for debugging (OpenTelemetry). Include this ID when contacting support about specific requests.",
        "schema": {
          "type": "string",
          "example": "abc123def456ghi789"
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Bad Request - Validation error or invalid request body.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "missingRequiredFields": {
                "summary": "Required fields not provided",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "Request validation failed",
                    "Fields": [
                      {
                        "Field": "TemplateKey",
                        "Message": "TemplateKey is required"
                      },
                      {
                        "Field": "EntityType",
                        "Message": "EntityType is required"
                      },
                      {
                        "Field": "Instruction",
                        "Message": "Instruction is required"
                      }
                    ]
                  }
                }
              },
              "invalidEntityType": {
                "summary": "EntityType is not a valid value",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "EntityType must be 'hotel' or 'room'"
                  }
                }
              },
              "invalidRenderLayout": {
                "summary": "RenderLayout is not a valid value",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "RenderLayout must be one of prose, highlights, roomcards, summary"
                  }
                }
              },
              "invalidBody": {
                "summary": "Malformed request body",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "Invalid request body"
                  }
                }
              }
            }
          }
        }
      },
      "Conflict": {
        "description": "Conflict - The template could not be saved because it conflicts with the current state of an existing template.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "templateConflict": {
                "summary": "Template conflict",
                "value": {
                  "Error": {
                    "Code": "ALREADY_EXISTS",
                    "Message": "Text template conflicts with an existing template"
                  }
                }
              }
            }
          }
        }
      },
      "Forbidden": {
        "description": "Forbidden - Valid API key but insufficient permissions for this operation. Also returned with code INSUFFICIENT_SCOPE when the API key does not carry both the content.authoring:write and content.generation:write permissions this endpoint requires.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "insufficientScope": {
                "summary": "Missing required scope",
                "value": {
                  "Error": {
                    "Code": "INSUFFICIENT_SCOPE",
                    "Message": "token lacks scope content.generation:write"
                  }
                }
              },
              "noProjectAccess": {
                "summary": "Project access required",
                "value": {
                  "Error": {
                    "Code": "FORBIDDEN",
                    "Message": "Project access required"
                  }
                }
              }
            }
          }
        }
      },
      "InternalServerError": {
        "description": "Internal Server Error - Unexpected server error. Include the X-Trace-ID header value when contacting support.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "internalError": {
                "summary": "Internal server error",
                "value": {
                  "Error": {
                    "Code": "INTERNAL_ERROR",
                    "Message": "Failed to save text template"
                  }
                }
              }
            }
          }
        }
      },
      "NotFound": {
        "description": "Not Found - The requested resource does not exist or is not in the calling project's scope.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "projectNotFound": {
                "summary": "Project not found",
                "value": {
                  "Error": {
                    "Code": "NOT_FOUND",
                    "Message": "Project not found"
                  }
                }
              }
            }
          }
        }
      },
      "TooManyRequests": {
        "description": "Too Many Requests. The response includes a `Retry-After` header with the number of seconds to wait before retrying.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          },
          "Retry-After": {
            "$ref": "#/components/headers/Retry-After"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "rateLimited": {
                "summary": "Limit exceeded",
                "value": {
                  "Error": {
                    "Code": "RATE_LIMIT_EXCEEDED",
                    "Message": "Rate limit exceeded. Retry after the number of seconds in the Retry-After header."
                  }
                }
              }
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Unauthorized - Invalid or missing API key.",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "invalidKey": {
                "summary": "Invalid API key",
                "value": {
                  "Error": {
                    "Code": "UNAUTHORIZED",
                    "Message": "Invalid API key"
                  }
                }
              }
            }
          }
        }
      }
    },
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Project-scoped API key"
      }
    }
  }
}
