{
  "openapi": "3.1.0",
  "info": {
    "title": "CardClan Integration API",
    "description": "The CardClan Integration API enables third-party applications and automation platforms to send personalized digital cards, manage workflows, and integrate CardClan's functionality into existing systems.",
    "version": "1.0.0",
    "license": {
      "name": "MIT"
    }
  },
  "servers": [
    {
      "url": "https://app.cardclan.io/api"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/integration/auth/create-key": {
      "post": {
        "tags": ["Authentication"],
        "summary": "Generate Integration Key",
        "description": "Creates a new integration API key for the authenticated user",
        "security": [
          {
            "firebaseAuth": []
          }
        ],
        "responses": {
          "201": {
            "description": "Integration key created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IntegrationKeyResponse"
                }
              }
            }
          },
          "400": {
            "description": "Integration key already exists",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "User not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/auth/validate": {
      "get": {
        "tags": ["Authentication"],
        "summary": "Validate Authentication",
        "description": "Validates the provided Bearer token and returns user information",
        "responses": {
          "200": {
            "description": "Authentication successful",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuthValidationResponse"
                }
              }
            }
          },
          "401": {
            "description": "Invalid or missing authentication",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Invalid token - user not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/workspaces": {
      "get": {
        "tags": ["Data Retrieval"],
        "summary": "Get User Workspaces",
        "description": "Retrieves all workspaces accessible to the authenticated user",
        "responses": {
          "200": {
            "description": "Workspaces retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WorkspacesResponse"
                }
              }
            }
          },
          "400": {
            "description": "Error fetching workspaces",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/cards": {
      "post": {
        "tags": ["Data Retrieval"],
        "summary": "Get User Cards",
        "description": "Retrieves all cards within a specific workspace",
        "parameters": [
          {
            "name": "workspace",
            "in": "query",
            "required": true,
            "description": "Workspace ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Cards retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CardsResponse"
                }
              }
            }
          },
          "400": {
            "description": "Workspace parameter is required",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/email-accounts": {
      "get": {
        "tags": ["Data Retrieval"],
        "summary": "Get Email Accounts",
        "description": "Retrieves all email accounts configured for the user's workspaces",
        "responses": {
          "200": {
            "description": "Email accounts retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EmailAccountsResponse"
                }
              }
            }
          },
          "400": {
            "description": "Error fetching email accounts",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/send-card": {
      "post": {
        "tags": ["Actions"],
        "summary": "Send Card",
        "description": "Sends a personalized card to a recipient via email",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SendCardRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Card sent successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SendCardResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/get-card-url": {
      "post": {
        "tags": ["Actions"],
        "summary": "Get Card URL",
        "description": "Generates a personalized card URL without sending an email",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/GetCardUrlRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Card URL generated successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GetCardUrlResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/config": {
      "post": {
        "tags": ["Configuration"],
        "summary": "Create Integration Configuration",
        "description": "Creates a new integration configuration for a specific card workflow",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateConfigRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Integration configuration created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateConfigResponse"
                }
              }
            }
          },
          "400": {
            "description": "Integration configuration already exists or invalid data",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "get": {
        "tags": ["Configuration"],
        "summary": "Get Integration Configuration",
        "description": "Retrieves integration configuration details by ID",
        "parameters": [
          {
            "name": "integrationId",
            "in": "query",
            "required": true,
            "description": "Integration configuration ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Integration configuration retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConfigResponse"
                }
              }
            }
          },
          "400": {
            "description": "Error fetching configuration",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/config/by-card": {
      "get": {
        "tags": ["Configuration"],
        "summary": "Get Configuration by Card",
        "description": "Retrieves integration configuration for a specific card",
        "parameters": [
          {
            "name": "cardId",
            "in": "query",
            "required": true,
            "description": "Card ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Integration configuration retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConfigByCardResponse"
                }
              }
            }
          },
          "409": {
            "description": "Integration configuration doesn't exist",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/workflow-cards": {
      "get": {
        "tags": ["Workflow Management"],
        "summary": "Get Cards with Workflows",
        "description": "Retrieves all cards that have active integration workflows",
        "responses": {
          "200": {
            "description": "Cards with workflows retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WorkflowCardsResponse"
                }
              }
            }
          },
          "400": {
            "description": "Error fetching cards with workflows",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/integration/card-details/{cardId}": {
      "get": {
        "tags": ["Workflow Management"],
        "summary": "Get Card Details",
        "description": "Retrieves detailed information about a specific card",
        "parameters": [
          {
            "name": "cardId",
            "in": "path",
            "required": true,
            "description": "Card ID",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Card details retrieved successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CardDetailsResponse"
                }
              }
            }
          },
          "404": {
            "description": "Card not found or access denied",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Enter your CardClan integration key"
      },
      "firebaseAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Firebase ID token for key generation"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string",
            "example": "Bad Request"
          },
          "message": {
            "type": "string",
            "example": "Card ID is required"
          },
          "statusCode": {
            "type": "number",
            "example": 400
          },
          "timestamp": {
            "type": "string",
            "format": "date-time",
            "example": "2024-01-15T10:30:00.000Z"
          }
        }
      },
      "IntegrationKeyResponse": {
        "type": "object",
        "properties": {
          "data": {
            "type": "string",
            "example": "550e8400-e29b-41d4-a716-446655440000"
          },
          "message": {
            "type": "string",
            "example": "Integration API key created for general platform!"
          },
          "platform": {
            "type": "string",
            "example": "general"
          }
        }
      },
      "AuthValidationResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "example": true
          },
          "message": {
            "type": "string",
            "example": "Bearer token authentication successful"
          },
          "user_id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a3"
          }
        }
      },
      "WorkspacesResponse": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "key": {
              "type": "string",
              "example": "workspace"
            },
            "choices": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/Workspace"
              }
            }
          }
        }
      },
      "Workspace": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a4"
          },
          "label": {
            "type": "string",
            "example": "Workspace Id"
          },
          "value": {
            "type": "string",
            "example": "My Business Workspace"
          },
          "id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a4"
          },
          "name": {
            "type": "string",
            "example": "My Business Workspace"
          }
        }
      },
      "CardsResponse": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "key": {
              "type": "string",
              "example": "card"
            },
            "choices": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/Card"
              }
            }
          }
        }
      },
      "Card": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "label": {
            "type": "string",
            "example": "Card Id"
          },
          "value": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "title": {
            "type": "string",
            "example": "Welcome Card"
          },
          "name": {
            "type": "string",
            "example": "Welcome Card"
          }
        }
      },
      "EmailAccountsResponse": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "key": {
              "type": "string",
              "example": "email-account"
            },
            "choices": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/EmailAccount"
              }
            }
          }
        }
      },
      "EmailAccount": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a6"
          },
          "value": {
            "type": "string",
            "example": "hello@company.com"
          },
          "label": {
            "type": "string",
            "example": "Email account"
          },
          "id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a6"
          },
          "name": {
            "type": "string",
            "example": "hello@company.com"
          }
        }
      },
      "SendCardRequest": {
        "type": "object",
        "required": ["card", "integrationId", "mergeTags"],
        "properties": {
          "card": {
            "type": "string",
            "description": "Card ID to send",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "emailAccount": {
            "type": "string",
            "description": "Email account ID or 'CardClan' for default",
            "example": "CardClan"
          },
          "integrationId": {
            "type": "string",
            "description": "Integration configuration ID",
            "example": "60f7b2b5b8f4a20015a4f5a7"
          },
          "mergeTags": {
            "type": "array",
            "description": "Array of merge tag objects with recipient data",
            "items": {
              "$ref": "#/components/schemas/MergeTags"
            },
            "minItems": 1
          }
        }
      },
      "GetCardUrlRequest": {
        "type": "object",
        "required": ["card", "integrationId", "mergeTags"],
        "properties": {
          "card": {
            "type": "string",
            "description": "Card ID for URL generation",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "integrationId": {
            "type": "string",
            "description": "Integration configuration ID",
            "example": "60f7b2b5b8f4a20015a4f5a7"
          },
          "mergeTags": {
            "type": "array",
            "description": "Array of merge tag objects with recipient data",
            "items": {
              "$ref": "#/components/schemas/MergeTags"
            },
            "minItems": 1
          }
        }
      },
      "MergeTags": {
        "type": "object",
        "required": ["name", "email"],
        "properties": {
          "name": {
            "type": "string",
            "description": "Recipient's full name",
            "example": "John Doe"
          },
          "email": {
            "type": "string",
            "format": "email",
            "description": "Recipient's email address",
            "example": "john@example.com"
          },
          "company": {
            "type": "string",
            "description": "Company name",
            "example": "Acme Corp"
          },
          "position": {
            "type": "string",
            "description": "Job title/position",
            "example": "Marketing Manager"
          }
        },
        "additionalProperties": {
          "type": "string",
          "description": "Additional custom merge tag fields"
        }
      },
      "SendCardResponse": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string",
            "example": "Card successfully sent to recipient: john@example.com"
          },
          "status": {
            "type": "boolean",
            "example": true
          },
          "recipient_id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a8"
          },
          "tracking_url": {
            "type": "string",
            "example": "https://cardclan.com/api/analytics/fetchImage/60f7b2b5b8f4a20015a4f5a8"
          }
        }
      },
      "GetCardUrlResponse": {
        "type": "object",
        "properties": {
          "view_card_url": {
            "type": "string",
            "example": "https://cardclan.com/view/60f7b2b5b8f4a20015a4f5a8"
          },
          "white_label_app_name": {
            "type": "string",
            "example": "CardClan"
          },
          "card_cover": {
            "type": "string",
            "example": "https://cdn.cardclan.com/covers/card-cover.jpg"
          },
          "unsubscribe_link_url": {
            "type": "string",
            "example": "https://cardclan.com/unsubscribe/60f7b2b5b8f4a20015a4f5a8"
          }
        }
      },
      "CreateConfigRequest": {
        "type": "object",
        "required": ["userId", "workspaceId", "cardId"],
        "properties": {
          "userId": {
            "type": "string",
            "description": "User ID",
            "example": "60f7b2b5b8f4a20015a4f5a3"
          },
          "workspaceId": {
            "type": "string",
            "description": "Workspace ID",
            "example": "60f7b2b5b8f4a20015a4f5a4"
          },
          "cardId": {
            "type": "string",
            "description": "Card ID",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          }
        }
      },
      "CreateConfigResponse": {
        "type": "object",
        "properties": {
          "data": {
            "$ref": "#/components/schemas/IntegrationConfig"
          },
          "message": {
            "type": "string",
            "example": "Integration configuration created"
          }
        }
      },
      "IntegrationConfig": {
        "type": "object",
        "properties": {
          "_id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a7"
          },
          "userId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a3"
          },
          "workspaceId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a4"
          },
          "cardId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time",
            "example": "2023-07-20T10:30:00.000Z"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time",
            "example": "2023-07-20T10:30:00.000Z"
          }
        }
      },
      "ConfigResponse": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "key": {
              "type": "string",
              "example": "items"
            },
            "cardId": {
              "type": "string",
              "example": "60f7b2b5b8f4a20015a4f5a5"
            },
            "emailAccount": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/EmailAccount"
              }
            },
            "mergeTags": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/MergeTagDefinition"
              }
            },
            "card": {
              "type": "object",
              "properties": {
                "cardId": {
                  "type": "string",
                  "example": "60f7b2b5b8f4a20015a4f5a5"
                },
                "title": {
                  "type": "string",
                  "example": "Welcome Card"
                }
              }
            }
          }
        }
      },
      "ConfigByCardResponse": {
        "type": "object",
        "properties": {
          "data": {
            "$ref": "#/components/schemas/IntegrationConfig"
          },
          "message": {
            "type": "string",
            "example": "Integration configuration"
          }
        }
      },
      "WorkflowCardsResponse": {
        "type": "object",
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WorkflowCard"
            }
          },
          "message": {
            "type": "string",
            "example": "User cards with workflows"
          }
        }
      },
      "WorkflowCard": {
        "type": "object",
        "properties": {
          "integrationId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a7"
          },
          "cardId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "workspaceId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a4"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time",
            "example": "2023-07-20T10:30:00.000Z"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time",
            "example": "2023-07-20T10:30:00.000Z"
          },
          "card": {
            "$ref": "#/components/schemas/CardWithMergeTags"
          },
          "workspace": {
            "$ref": "#/components/schemas/WorkspaceInfo"
          }
        }
      },
      "CardWithMergeTags": {
        "type": "object",
        "properties": {
          "_id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a5"
          },
          "integrationId": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a7"
          },
          "title": {
            "type": "string",
            "example": "Welcome Card"
          },
          "mergeTags": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MergeTagDefinition"
            }
          }
        }
      },
      "WorkspaceInfo": {
        "type": "object",
        "properties": {
          "_id": {
            "type": "string",
            "example": "60f7b2b5b8f4a20015a4f5a4"
          },
          "name": {
            "type": "string",
            "example": "My Business Workspace"
          }
        }
      },
      "CardDetailsResponse": {
        "type": "object",
        "properties": {
          "data": {
            "type": "object",
            "properties": {
              "card": {
                "$ref": "#/components/schemas/CardWithMergeTags"
              },
              "workspace": {
                "type": "object",
                "properties": {
                  "_id": {
                    "type": "string",
                    "example": "60f7b2b5b8f4a20015a4f5a4"
                  },
                  "name": {
                    "type": "string",
                    "example": "My Business Workspace"
                  },
                  "owner_id": {
                    "type": "string",
                    "example": "60f7b2b5b8f4a20015a4f5a3"
                  }
                }
              }
            }
          },
          "message": {
            "type": "string",
            "example": "Card details"
          }
        }
      },
      "MergeTagDefinition": {
        "type": "object",
        "properties": {
          "tagName": {
            "type": "string",
            "example": "name"
          },
          "type": {
            "type": "string",
            "example": "text"
          }
        }
      }
    }
  }
}