Spaces:
Runtime error
Runtime error
File size: 14,154 Bytes
61fe5a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | # AI Lead Qualification Bot - API Documentation
## Overview
The AI Lead Qualification Bot provides a comprehensive API for lead qualification, conversation management, and CRM integration. This document describes all available endpoints, request/response formats, and usage examples.
## Base URL
```
https://your-domain.com/api/v1
```
## Authentication
All API requests require authentication using API keys. Include your API key in the request headers:
```
Authorization: Bearer YOUR_API_KEY
```
## Rate Limits
- **Free Tier**: 100 requests/hour
- **Professional Tier**: 1,000 requests/hour
- **Enterprise Tier**: 10,000 requests/hour
## Endpoints
### 1. Conversation Management
#### Start Conversation
**POST** `/conversations/start`
Start a new conversation and get a conversation ID.
**Request:**
```json
{
"user_id": "optional_user_id",
"source": "website|email|social|api",
"initial_context": {
"referrer": "google",
"campaign": "summer_2024",
"landing_page": "/pricing"
}
}
```
**Response:**
```json
{
"conversation_id": "conv_123456789",
"greeting": "Hello! I'm here to help you find the right solution...",
"status": "active",
"created_at": "2024-01-15T10:30:00Z"
}
```
#### Send Message
**POST** `/conversations/{conversation_id}/message`
Send a message in an existing conversation.
**Request:**
```json
{
"message": "Hi, I'm looking for a CRM solution for our sales team",
"metadata": {
"user_agent": "Mozilla/5.0...",
"ip_address": "192.168.1.1",
"session_id": "sess_123"
}
}
```
**Response:**
```json
{
"conversation_id": "conv_123456789",
"response": "Hello! I'd be happy to help you find the right CRM solution...",
"structured_output": {
"lead": {
"name": "Sarah",
"email": null,
"company": "TechCorp",
"role": "Sales Director",
"industry": "Technology"
},
"intent": "buy_soon",
"score": 85,
"top_signals": [
"budget mentioned",
"timeline specified",
"pain points clear"
],
"recommended_action": "schedule_demo",
"explain": "High-scoring lead with clear buying intent",
"crm_tags": [
"enterprise",
"high_priority",
"hot_lead"
]
},
"timestamp": "2024-01-15T10:31:00Z"
}
```
#### Get Conversation History
**GET** `/conversations/{conversation_id}/history`
Retrieve the full conversation history.
**Response:**
```json
{
"conversation_id": "conv_123456789",
"messages": [
{
"role": "user",
"content": "Hi, I'm looking for a CRM solution",
"timestamp": "2024-01-15T10:30:00Z"
},
{
"role": "assistant",
"content": "Hello! I'd be happy to help...",
"timestamp": "2024-01-15T10:30:05Z"
}
],
"lead_info": {
"name": "Sarah",
"company": "TechCorp",
"role": "Sales Director"
},
"current_score": 85,
"current_intent": "buy_soon"
}
```
#### End Conversation
**POST** `/conversations/{conversation_id}/end`
End a conversation and get a summary.
**Response:**
```json
{
"conversation_id": "conv_123456789",
"summary": "Conversation ended successfully",
"final_score": 85,
"final_intent": "buy_soon",
"total_messages": 8,
"duration_minutes": 15
}
```
### 2. Lead Qualification
#### Get Lead Score
**POST** `/leads/score`
Get a lead score for a conversation or lead data.
**Request:**
```json
{
"conversation_id": "conv_123456789",
"lead_data": {
"name": "Sarah",
"company": "TechCorp",
"role": "Sales Director",
"industry": "Technology",
"team_size": 50
},
"behavioral_data": {
"pages_visited": 5,
"trial_usage": 0.2,
"email_opens": 3,
"demo_requested": true
}
}
```
**Response:**
```json
{
"score": 85,
"intent": "buy_soon",
"confidence": 0.92,
"signals": [
"budget mentioned",
"timeline specified",
"decision maker identified"
],
"recommendation": "schedule_demo",
"explanation": "High-scoring lead with clear buying intent"
}
```
#### Batch Lead Scoring
**POST** `/leads/batch-score`
Score multiple leads at once.
**Request:**
```json
{
"leads": [
{
"conversation_id": "conv_123456789",
"lead_data": {...},
"behavioral_data": {...}
},
{
"conversation_id": "conv_987654321",
"lead_data": {...},
"behavioral_data": {...}
}
]
}
```
**Response:**
```json
{
"results": [
{
"conversation_id": "conv_123456789",
"score": 85,
"intent": "buy_soon",
"recommendation": "schedule_demo"
},
{
"conversation_id": "conv_987654321",
"score": 35,
"intent": "researching",
"recommendation": "nurture_email"
}
]
}
```
### 3. CRM Integration
#### Create Lead in CRM
**POST** `/crm/leads`
Create a lead in connected CRM systems.
**Request:**
```json
{
"conversation_id": "conv_123456789",
"lead_data": {
"name": "Sarah Johnson",
"email": "sarah@techcorp.com",
"company": "TechCorp",
"role": "Sales Director",
"industry": "Technology",
"phone": "+1-555-0123"
},
"qualification_data": {
"score": 85,
"intent": "buy_soon",
"signals": ["budget mentioned", "timeline specified"],
"recommendation": "schedule_demo"
},
"crm_systems": ["hubspot", "salesforce"]
}
```
**Response:**
```json
{
"success": true,
"results": {
"hubspot": {
"success": true,
"lead_id": "hubspot_123456",
"message": "Lead created successfully"
},
"salesforce": {
"success": true,
"lead_id": "salesforce_789012",
"message": "Lead created successfully"
}
}
}
```
#### Update Lead
**PUT** `/crm/leads/{lead_id}`
Update an existing lead in CRM systems.
**Request:**
```json
{
"lead_data": {
"score": 90,
"intent": "buy_soon",
"status": "qualified"
},
"crm_systems": ["hubspot", "salesforce"]
}
```
**Response:**
```json
{
"success": true,
"results": {
"hubspot": {
"success": true,
"message": "Lead updated successfully"
},
"salesforce": {
"success": true,
"message": "Lead updated successfully"
}
}
}
```
#### Add Note to Lead
**POST** `/crm/leads/{lead_id}/notes`
Add a note to a lead in CRM systems.
**Request:**
```json
{
"note": "Lead showed strong buying intent during conversation. Budget: $100k, Timeline: 1 month",
"crm_systems": ["hubspot", "salesforce"]
}
```
**Response:**
```json
{
"success": true,
"results": {
"hubspot": {
"success": true,
"note_id": "note_123456",
"message": "Note added successfully"
},
"salesforce": {
"success": true,
"note_id": "note_789012",
"message": "Note added successfully"
}
}
}
```
### 4. Knowledge Base
#### Search Knowledge Base
**POST** `/knowledge/search`
Search the knowledge base for relevant information.
**Request:**
```json
{
"query": "pricing information for enterprise customers",
"document_types": ["product_docs", "case_studies"],
"limit": 5
}
```
**Response:**
```json
{
"results": [
{
"content": "Our Enterprise plan starts at $2,000/month...",
"source": "pricing_guide.txt",
"document_type": "product_docs",
"score": 0.95
},
{
"content": "TechCorp saw 300% increase in lead quality...",
"source": "techcorp_case_study.txt",
"document_type": "case_studies",
"score": 0.87
}
]
}
```
#### Add Document to Knowledge Base
**POST** `/knowledge/documents`
Add a new document to the knowledge base.
**Request:**
```json
{
"content": "New product feature documentation...",
"document_type": "product_docs",
"metadata": {
"title": "New Feature Guide",
"author": "Product Team",
"version": "1.0"
}
}
```
**Response:**
```json
{
"success": true,
"document_id": "doc_123456",
"message": "Document added successfully"
}
```
### 5. Model Management
#### Train Model
**POST** `/models/train`
Retrain the predictive model with new data.
**Request:**
```json
{
"training_data": [
{
"conversation_data": {...},
"outcome": "converted",
"conversion_value": 50000
}
],
"model_type": "lead_scoring",
"parameters": {
"learning_rate": 0.1,
"max_depth": 6
}
}
```
**Response:**
```json
{
"success": true,
"model_id": "model_123456",
"accuracy": 0.85,
"training_time": "2.5 minutes",
"message": "Model trained successfully"
}
```
#### Get Model Performance
**GET** `/models/{model_id}/performance`
Get performance metrics for a trained model.
**Response:**
```json
{
"model_id": "model_123456",
"accuracy": 0.85,
"precision": 0.82,
"recall": 0.88,
"f1_score": 0.85,
"feature_importance": {
"budget_mentioned": 0.15,
"timeline_mentioned": 0.12,
"decision_maker": 0.20
},
"last_updated": "2024-01-15T10:00:00Z"
}
```
### 6. Analytics
#### Get Conversation Analytics
**GET** `/analytics/conversations`
Get analytics for conversations.
**Query Parameters:**
- `start_date`: Start date (YYYY-MM-DD)
- `end_date`: End date (YYYY-MM-DD)
- `intent`: Filter by intent
- `score_range`: Filter by score range
**Response:**
```json
{
"total_conversations": 1250,
"average_score": 65.5,
"intent_distribution": {
"buy_soon": 0.25,
"considering": 0.35,
"researching": 0.30,
"not_interested": 0.10
},
"conversion_rate": 0.18,
"average_conversation_length": 8.5,
"top_signals": [
"budget mentioned",
"timeline specified",
"pain points clear"
]
}
```
#### Get Lead Quality Metrics
**GET** `/analytics/lead-quality`
Get lead quality metrics.
**Response:**
```json
{
"total_leads": 1250,
"qualified_leads": 450,
"conversion_rate": 0.18,
"average_lead_score": 65.5,
"score_distribution": {
"high_priority": 0.25,
"medium_priority": 0.45,
"low_priority": 0.30
},
"top_industries": [
"Technology",
"Healthcare",
"Financial Services"
]
}
```
## Error Handling
All API endpoints return appropriate HTTP status codes and error messages.
### Error Response Format
```json
{
"error": {
"code": "INVALID_REQUEST",
"message": "Invalid conversation ID provided",
"details": {
"conversation_id": "must be a valid UUID"
}
},
"timestamp": "2024-01-15T10:30:00Z"
}
```
### Common Error Codes
- `400`: Bad Request - Invalid input data
- `401`: Unauthorized - Invalid or missing API key
- `403`: Forbidden - Insufficient permissions
- `404`: Not Found - Resource not found
- `429`: Too Many Requests - Rate limit exceeded
- `500`: Internal Server Error - Server error
## Webhooks
### Configure Webhook
**POST** `/webhooks/configure`
Configure webhook endpoints for real-time notifications.
**Request:**
```json
{
"url": "https://your-domain.com/webhook",
"events": ["lead_created", "lead_updated", "conversation_ended"],
"secret": "your_webhook_secret"
}
```
### Webhook Events
#### Lead Created
```json
{
"event": "lead_created",
"conversation_id": "conv_123456789",
"lead_data": {
"name": "Sarah",
"company": "TechCorp",
"score": 85,
"intent": "buy_soon"
},
"timestamp": "2024-01-15T10:30:00Z"
}
```
#### Lead Updated
```json
{
"event": "lead_updated",
"conversation_id": "conv_123456789",
"lead_data": {
"score": 90,
"intent": "buy_soon",
"recommendation": "schedule_demo"
},
"timestamp": "2024-01-15T10:35:00Z"
}
```
## SDKs and Libraries
### Python SDK
```python
from ai_lead_qualification import LeadQualificationBot
bot = LeadQualificationBot(api_key="your_api_key")
# Start conversation
conversation = bot.start_conversation()
# Send message
response = bot.send_message(
conversation_id=conversation.id,
message="Hi, I'm looking for a CRM solution"
)
print(response.structured_output.score)
```
### JavaScript SDK
```javascript
const LeadQualificationBot = require('ai-lead-qualification');
const bot = new LeadQualificationBot('your_api_key');
// Start conversation
const conversation = await bot.startConversation();
// Send message
const response = await bot.sendMessage(
conversation.id,
'Hi, I\'m looking for a CRM solution'
);
console.log(response.structuredOutput.score);
```
## Rate Limiting
Rate limits are applied per API key and reset every hour. When you exceed the rate limit, you'll receive a `429 Too Many Requests` response.
### Rate Limit Headers
```
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 950
X-RateLimit-Reset: 1642233600
```
## Best Practices
### 1. Conversation Management
- Always start a conversation before sending messages
- Store conversation IDs for session management
- End conversations when users leave
### 2. Error Handling
- Implement proper error handling for all API calls
- Retry failed requests with exponential backoff
- Log errors for debugging
### 3. Rate Limiting
- Monitor rate limit headers
- Implement request queuing for high-volume applications
- Use batch endpoints when possible
### 4. Security
- Keep API keys secure and rotate regularly
- Use HTTPS for all API calls
- Validate webhook signatures
### 5. Performance
- Cache conversation data when appropriate
- Use batch endpoints for multiple operations
- Implement connection pooling
## Support
For API support and questions:
- **Email**: api-support@aileadqualification.com
- **Documentation**: https://docs.aileadqualification.com
- **Status Page**: https://status.aileadqualification.com
- **Community**: https://community.aileadqualification.com
|