{
  "openapi": "3.0.0",
  "info": {
    "title": "YGO API: Save tag",
    "version": "2.0.0"
  },
  "servers": [
    {
      "url": "https://api.ygo.ai",
      "description": "Production and Sandbox (determined by API key)"
    }
  ],
  "paths": {
    "/v2/project/content/saveTag": {
      "post": {
        "summary": "Save tag",
        "description": "Upsert a project tag. Omit TagID to create a new tag or supply an existing TagID to rename it. The name must be unique within the project (case-insensitive); a duplicate returns 409, and renaming a tag id not in the project returns 404.",
        "operationId": "saveTag",
        "tags": [
          "Content Enrichment API/Tags"
        ],
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/X-Project-ID"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SaveTagRequest"
              },
              "examples": {
                "create": {
                  "summary": "Create a new tag (no TagID)",
                  "value": {
                    "Name": "Summer Campaign"
                  }
                },
                "rename": {
                  "summary": "Rename an existing tag (TagID + new Name)",
                  "value": {
                    "TagID": "0192fc01-1111-7abc-8def-aaaaaaaaaaaa",
                    "Name": "Summer 2026 Campaign"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Tag created or renamed successfully.",
            "headers": {
              "X-Trace-ID": {
                "$ref": "#/components/headers/X-Trace-ID"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SaveTagResponse"
                },
                "examples": {
                  "create": {
                    "summary": "A newly created tag",
                    "value": {
                      "Data": {
                        "ID": "0192fc01-1111-7abc-8def-aaaaaaaaaaaa",
                        "ProjectID": "0192fa00-0000-7abc-8def-000000000000",
                        "Name": "Summer Campaign",
                        "CreatedBy": "editor@example.com"
                      }
                    }
                  },
                  "rename": {
                    "summary": "A renamed tag (CreatedBy preserved)",
                    "value": {
                      "Data": {
                        "ID": "0192fc01-1111-7abc-8def-aaaaaaaaaaaa",
                        "ProjectID": "0192fa00-0000-7abc-8def-000000000000",
                        "Name": "Summer 2026 Campaign",
                        "CreatedBy": "editor@example.com"
                      }
                    }
                  }
                }
              }
            }
          },
          "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": "TagID"
          },
          "Message": {
            "type": "string",
            "description": "Human-readable validation error message for this field.",
            "example": "TagID must be a valid UUID"
          },
          "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
            }
          }
        }
      },
      "ProjectTag": {
        "type": "object",
        "required": [
          "ID",
          "ProjectID",
          "Name",
          "CreatedBy"
        ],
        "description": "A project-private internal tag.",
        "properties": {
          "ID": {
            "type": "string",
            "format": "uuid",
            "description": "Unique tag UUID.",
            "example": "0192fc01-1111-7abc-8def-aaaaaaaaaaaa"
          },
          "ProjectID": {
            "type": "string",
            "format": "uuid",
            "description": "UUID of the project that owns the tag.",
            "example": "0192fa00-0000-7abc-8def-000000000000"
          },
          "Name": {
            "type": "string",
            "description": "Tag display name.",
            "example": "Summer Campaign"
          },
          "CreatedBy": {
            "type": "string",
            "description": "Audit identity of who created the tag: a user token records the user's email; a project (proj_) machine token records its project-scoped identity (e.g. 'project:<project-uuid>').",
            "example": "editor@example.com"
          }
        }
      },
      "SaveTagRequest": {
        "type": "object",
        "required": [
          "Name"
        ],
        "description": "Request body for creating or renaming a project tag.",
        "properties": {
          "TagID": {
            "type": "string",
            "format": "uuid",
            "description": "UUID of an existing tag to rename. Omit (or send empty) to create a new tag. When supplied it must be a valid, non-nil UUID belonging to the calling project; a tag id not in the project returns 404.",
            "example": "0192fc01-1111-7abc-8def-aaaaaaaaaaaa"
          },
          "Name": {
            "type": "string",
            "description": "Tag display name. Required, non-empty after trimming, and unique within the project (case-insensitive). Capped at 200 Unicode characters.",
            "example": "Summer Campaign"
          }
        }
      },
      "SaveTagResponse": {
        "type": "object",
        "required": [
          "Data"
        ],
        "description": "Single-object response envelope wrapping the saved tag.",
        "properties": {
          "Data": {
            "$ref": "#/components/schemas/ProjectTag"
          }
        }
      }
    },
    "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": {
              "invalidTagID": {
                "summary": "TagID provided but not a valid UUID",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "Request validation failed",
                    "Fields": [
                      {
                        "Field": "TagID",
                        "Message": "TagID must be a valid UUID"
                      }
                    ]
                  }
                }
              },
              "emptyName": {
                "summary": "Name is missing or blank",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "Name cannot be empty"
                  }
                }
              },
              "invalidBody": {
                "summary": "Malformed request body",
                "value": {
                  "Error": {
                    "Code": "VALIDATION_ERROR",
                    "Message": "Invalid request body"
                  }
                }
              }
            }
          }
        }
      },
      "Conflict": {
        "description": "Conflict - A tag with this name already exists in the project (case-insensitive).",
        "headers": {
          "X-Trace-ID": {
            "$ref": "#/components/headers/X-Trace-ID"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            },
            "examples": {
              "duplicateName": {
                "summary": "Duplicate tag name",
                "value": {
                  "Error": {
                    "Code": "ALREADY_EXISTS",
                    "Message": "a tag with this name already exists in this project"
                  }
                }
              }
            }
          }
        }
      },
      "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 the content.catalog:write permission 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.catalog: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": "Internal error"
                  }
                }
              }
            }
          }
        }
      },
      "NotFound": {
        "description": "Not Found - The tag to rename 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": {
              "tagNotFound": {
                "summary": "Tag id not in the project",
                "value": {
                  "Error": {
                    "Code": "NOT_FOUND",
                    "Message": "Tag 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"
      }
    }
  }
}
