# Chat Widget

## Get Chat Widget Script

> Returns the JavaScript code needed to embed the chat widget on a website

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/scripts/chat-widget.js":{"get":{"summary":"Get Chat Widget Script","description":"Returns the JavaScript code needed to embed the chat widget on a website","tags":["Chat Widget"],"responses":{"200":{"description":"JavaScript code for chat widget","content":{"application/javascript":{"schema":{"type":"string","description":"JavaScript code"}}}}}}}}}
```

## Get Chat Widget Configuration

> Returns the configuration for the chat widget

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/chat-widget-config":{"get":{"summary":"Get Chat Widget Configuration","description":"Returns the configuration for the chat widget","tags":["Chat Widget"],"parameters":[{"name":"configId","in":"query","required":true,"schema":{"type":"string"},"description":"ID of the chat widget configuration"}],"responses":{"200":{"description":"Chat widget configuration","content":{"application/json":{"schema":{"type":"object","properties":{"success":{"type":"boolean"},"data":{"type":"object","description":"Widget configuration data"}}}}}},"404":{"description":"Configuration not found","content":{"application/json":{"schema":{"type":"object","properties":{"success":{"type":"boolean"},"error":{"type":"string"}}}}}}}}}}}
```

## Get Chat Widget

> Returns the HTML for the chat widget with the specified configuration

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/chat-widget/{configId}":{"get":{"summary":"Get Chat Widget","description":"Returns the HTML for the chat widget with the specified configuration","tags":["Chat Widget"],"parameters":[{"name":"configId","in":"path","required":true,"schema":{"type":"string"},"description":"ID of the chat widget configuration"}],"responses":{"200":{"description":"HTML content for the chat widget","content":{"text/html":{"schema":{"type":"string","description":"HTML content"}}}},"404":{"description":"Widget not found","content":{"text/plain":{"schema":{"type":"string"}}}}}}}}}
```

## Get Chat Widget with From ID

> Returns the HTML for the chat widget with the specified configuration and from ID

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/chat-widget/{configId}/{fromId}":{"get":{"summary":"Get Chat Widget with From ID","description":"Returns the HTML for the chat widget with the specified configuration and from ID","tags":["Chat Widget"],"parameters":[{"name":"configId","in":"path","required":true,"schema":{"type":"string"},"description":"ID of the chat widget configuration"},{"name":"fromId","in":"path","required":true,"schema":{"type":"string"},"description":"ID of the source (for tracking purposes)"}],"responses":{"200":{"description":"HTML content for the chat widget","content":{"text/html":{"schema":{"type":"string","description":"HTML content"}}}},"404":{"description":"Widget not found","content":{"text/plain":{"schema":{"type":"string"}}}}}}}}}
```

## Handle Incoming Chat Widget Message

> Processes an incoming message from the chat widget and publishes it to a PubSub topic

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/incoming-chat-widget-message":{"post":{"summary":"Handle Incoming Chat Widget Message","description":"Processes an incoming message from the chat widget and publishes it to a PubSub topic","tags":["Chat Widget","Messages"],"parameters":[{"name":"userId","in":"query","required":true,"schema":{"type":"string"},"description":"ID of the user/owner to whom the chat widget belongs"}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","required":["customData","messageType"],"properties":{"customData":{"type":"object","required":["body"],"properties":{"messageSid":{"type":"string","description":"Unique identifier for the message"},"id":{"type":"string","description":"Alternative unique identifier for the message"},"fromId":{"type":"string","description":"ID of the sender (visitor)"},"toId":{"type":"string","description":"ID of the chat widget"},"body":{"type":"string","description":"Message content"},"status":{"type":"string","description":"Message status","default":"received"},"mediaUrl":{"type":["string","null"],"description":"URL to media content (if any)"},"mediaContentType":{"type":["string","null"],"description":"MIME type of media content"}}},"messageType":{"type":"string","description":"Type of message (e.g., text, reaction)","default":"text"}}}}}},"responses":{"200":{"description":"Message processed successfully","content":{"application/json":{"schema":{"type":"object","properties":{"success":{"type":"boolean"},"messageId":{"type":"string","description":"PubSub message ID"}}}}}},"400":{"description":"Invalid request","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"},"details":{"type":"string","description":"Additional error details"}}}}}},"405":{"description":"Method not allowed","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"}}}}}},"500":{"description":"Server error","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"}}}}}}}}}}}
```

## Get Chat Widget Messages

> Retrieves messages for a specific chat widget conversation

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/chat-widget-messages":{"get":{"summary":"Get Chat Widget Messages","description":"Retrieves messages for a specific chat widget conversation","tags":["Chat Widget","Messages"],"parameters":[{"name":"fromId","in":"query","required":true,"schema":{"type":"string"},"description":"ID of the user/owner of the contact"},{"name":"toId","in":"query","required":true,"schema":{"type":"string"},"description":"ID of the chat widget that the contact is using"},{"name":"after","in":"query","required":false,"schema":{"type":"string","format":"int64"},"description":"Optional timestamp (in milliseconds) to fetch only messages after this time"}],"responses":{"200":{"description":"List of messages with bot typing status","content":{"application/json":{"schema":{"type":"object","properties":{"messages":{"type":"array","items":{"type":"object","properties":{"messageSid":{"type":"string","description":"Unique message identifier"},"body":{"type":"string","description":"Message content"},"mediaUrl":{"type":["string","null"],"description":"URL to media content (if any)"},"mediaContentType":{"type":["string","null"],"description":"MIME type of media content"},"timestamp":{"type":"string","format":"date-time","description":"Message timestamp"},"type":{"type":"string","enum":["text","image"],"description":"Message type"},"direction":{"type":"string","enum":["inbound","outbound","outbound-api"],"description":"Message direction (inbound = from user, outbound = from system)"},"status":{"type":"string","description":"Message delivery status"}}}},"isBotTyping":{"type":"boolean","description":"Whether the bot is currently typing a response"}}}}}},"400":{"description":"Invalid request","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"}}}}}},"404":{"description":"Contact not found","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"}}}}}},"500":{"description":"Server error","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"},"details":{"type":"string","description":"Error details (only provided in development environment)"}}}}}}}}}}}
```

## Mark Chat Widget Messages as Read

> Updates the read status of all unread messages for a specific chat widget conversation

```json
{"openapi":"3.1.0","info":{"title":"DM Champ API","version":"1.0.0"},"servers":[{"url":"https://api.dmchamp.com/v1","description":"Production API Server"}],"security":[{"ApiKeyAuth":[]},{"UserIdAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"type":"apiKey","in":"query","name":"apiKey","description":"Your DM Champ API key. Obtain from Settings → API Keys."},"UserIdAuth":{"type":"apiKey","in":"query","name":"userId","description":"Your DM Champ User ID. Alternative to apiKey authentication. Cannot be used for agency-only endpoints."}}},"paths":{"/update-message-read-status":{"post":{"summary":"Mark Chat Widget Messages as Read","description":"Updates the read status of all unread messages for a specific chat widget conversation","tags":["Chat Widget","Messages"],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","required":["fromId","toId"],"properties":{"fromId":{"type":"string","description":"ID of the user/owner of the contact"},"toId":{"type":"string","description":"ID of the chat widget that the contact is using"}}}}}},"responses":{"200":{"description":"Messages marked as read successfully","content":{"application/json":{"schema":{"type":"object","properties":{"success":{"type":"boolean"}}}}}},"400":{"description":"Missing required parameters","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"}}}}}},"404":{"description":"Contact not found","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"}}}}}},"500":{"description":"Server error","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"},"details":{"type":"string","description":"Error details (only provided in development environment)"}}}}}}}}}}}
```
