File size: 21,398 Bytes
d7fe8fb 3d5a179 d7fe8fb dcd1e34 3d5a179 d7fe8fb | 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 | import React, { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import {
Key,
Plus,
Trash2,
Copy,
Check,
Eye,
EyeOff,
Code,
FileText,
AlertCircle,
Clock,
Sparkles,
ExternalLink,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import { cn } from "@/lib/utils";
import { createAPIKey, listAPIKeys, deleteAPIKey } from "@/services/api";
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "https://seth0330-ezofisocr.hf.space";
export default function APIKeys() {
const [apiKeys, setApiKeys] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
const [showCreateModal, setShowCreateModal] = useState(false);
const [newKeyName, setNewKeyName] = useState("");
const [isCreating, setIsCreating] = useState(false);
const [newlyCreatedKey, setNewlyCreatedKey] = useState(null);
const [copiedKeyId, setCopiedKeyId] = useState(null);
const [visibleKeys, setVisibleKeys] = useState(new Set());
// Fetch API keys on mount
useEffect(() => {
fetchAPIKeys();
}, []);
const fetchAPIKeys = async () => {
setIsLoading(true);
setError(null);
try {
const response = await listAPIKeys();
setApiKeys(response.api_keys || []);
} catch (err) {
setError(err.message || "Failed to load API keys");
} finally {
setIsLoading(false);
}
};
const handleCreateKey = async () => {
if (!newKeyName.trim()) {
setError("Please enter a name for your API key");
return;
}
setIsCreating(true);
setError(null);
try {
const response = await createAPIKey(newKeyName.trim());
setNewlyCreatedKey(response.api_key);
setNewKeyName("");
setShowCreateModal(false);
await fetchAPIKeys();
} catch (err) {
setError(err.message || "Failed to create API key");
} finally {
setIsCreating(false);
}
};
const handleDeleteKey = async (keyId) => {
if (!confirm("Are you sure you want to deactivate this API key? This action cannot be undone.")) {
return;
}
try {
await deleteAPIKey(keyId);
await fetchAPIKeys();
} catch (err) {
setError(err.message || "Failed to delete API key");
}
};
const copyToClipboard = (text, keyId = null) => {
navigator.clipboard.writeText(text);
if (keyId) {
setCopiedKeyId(keyId);
setTimeout(() => setCopiedKeyId(null), 2000);
}
};
const toggleKeyVisibility = (keyId) => {
const newVisible = new Set(visibleKeys);
if (newVisible.has(keyId)) {
newVisible.delete(keyId);
} else {
newVisible.add(keyId);
}
setVisibleKeys(newVisible);
};
const formatDate = (dateString) => {
if (!dateString) return "Never";
const date = new Date(dateString);
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
});
};
return (
<div className="min-h-screen bg-[#FAFAFA]">
{/* Header */}
<header className="bg-white border-b border-slate-200/80 sticky top-0 z-40 h-16">
<div className="px-8 h-full flex items-center justify-between">
<div>
<h1 className="text-xl font-bold text-slate-900 tracking-tight leading-tight">
API Keys
</h1>
<p className="text-sm text-slate-500 leading-tight">
Manage API keys for external application access
</p>
</div>
<Button
onClick={() => setShowCreateModal(true)}
className="h-10 px-6 rounded-xl font-semibold bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-700 hover:to-violet-700 shadow-lg shadow-indigo-500/25 hover:shadow-xl hover:shadow-indigo-500/30 transition-all duration-300"
>
<Plus className="h-4 w-4 mr-2" />
Create API Key
</Button>
</div>
</header>
{/* Main Content */}
<div className="p-8">
{/* Error Message */}
{error && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
className="max-w-4xl mx-auto mb-6"
>
<div className="bg-red-50 border border-red-200 rounded-2xl p-4 flex items-start gap-3">
<AlertCircle className="h-5 w-5 text-red-600 flex-shrink-0 mt-0.5" />
<div className="flex-1">
<h3 className="font-semibold text-red-900 mb-1">Error</h3>
<p className="text-sm text-red-700">{error}</p>
</div>
<button
onClick={() => setError(null)}
className="text-red-400 hover:text-red-600 transition-colors"
>
×
</button>
</div>
</motion.div>
)}
{/* Success Message for New Key */}
{newlyCreatedKey && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
className="max-w-4xl mx-auto mb-6"
>
<div className="bg-emerald-50 border border-emerald-200 rounded-2xl p-6">
<div className="flex items-start gap-3 mb-4">
<div className="h-10 w-10 rounded-xl bg-emerald-100 flex items-center justify-center flex-shrink-0">
<Check className="h-5 w-5 text-emerald-600" />
</div>
<div className="flex-1">
<h3 className="font-semibold text-emerald-900 mb-1">
API Key Created Successfully!
</h3>
<p className="text-sm text-emerald-700 mb-4">
⚠️ Store this key securely - it will not be shown again.
</p>
<div className="bg-white rounded-xl p-4 border border-emerald-200">
<div className="flex items-center gap-2 mb-2">
<Key className="h-4 w-4 text-slate-500" />
<span className="text-xs font-medium text-slate-500 uppercase tracking-wide">
Your API Key
</span>
</div>
<div className="flex items-center gap-2">
<code className="flex-1 font-mono text-sm text-slate-900 break-all">
{newlyCreatedKey}
</code>
<Button
size="sm"
variant="outline"
onClick={() => copyToClipboard(newlyCreatedKey, "new")}
className="flex-shrink-0"
>
{copiedKeyId === "new" ? (
<Check className="h-4 w-4 text-emerald-600" />
) : (
<Copy className="h-4 w-4" />
)}
</Button>
</div>
</div>
</div>
</div>
<Button
onClick={() => setNewlyCreatedKey(null)}
className="w-full bg-emerald-600 hover:bg-emerald-700 text-white"
>
I've Saved My Key
</Button>
</div>
</motion.div>
)}
{/* API Keys List */}
<div className="max-w-4xl mx-auto">
{isLoading ? (
<div className="bg-white rounded-2xl border border-slate-200 p-12 text-center">
<div className="h-12 w-12 mx-auto rounded-2xl bg-indigo-100 flex items-center justify-center mb-4 animate-pulse">
<Key className="h-6 w-6 text-indigo-600" />
</div>
<p className="text-slate-600">Loading API keys...</p>
</div>
) : apiKeys.length === 0 ? (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
className="bg-white rounded-2xl border border-slate-200 p-12 text-center"
>
<div className="h-16 w-16 mx-auto rounded-2xl bg-slate-100 flex items-center justify-center mb-4">
<Key className="h-8 w-8 text-slate-400" />
</div>
<h3 className="text-lg font-semibold text-slate-900 mb-2">
No API Keys Yet
</h3>
<p className="text-slate-500 mb-6">
Create your first API key to start using the API from external applications
</p>
<Button
onClick={() => setShowCreateModal(true)}
className="h-11 px-6 rounded-xl font-semibold bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-700 hover:to-violet-700"
>
<Plus className="h-4 w-4 mr-2" />
Create Your First API Key
</Button>
</motion.div>
) : (
<div className="space-y-4">
{apiKeys.map((key) => (
<motion.div
key={key.id}
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
className="bg-white rounded-2xl border border-slate-200 p-6 hover:shadow-lg transition-shadow"
>
<div className="flex items-start justify-between mb-4">
<div className="flex-1">
<div className="flex items-center gap-3 mb-2">
<Key className="h-5 w-5 text-indigo-600" />
<h3 className="font-semibold text-slate-900">{key.name}</h3>
{key.is_active ? (
<Badge className="bg-emerald-50 text-emerald-700 border-emerald-200">
Active
</Badge>
) : (
<Badge className="bg-slate-100 text-slate-600 border-slate-200">
Inactive
</Badge>
)}
</div>
<div className="ml-8 space-y-2">
<div className="flex items-center gap-2">
<span className="font-mono text-sm text-slate-500 break-all">
{key.key_prefix}
</span>
</div>
<div className="flex items-center gap-4 text-xs text-slate-400">
<div className="flex items-center gap-1">
<Clock className="h-3 w-3" />
<span>Created: {formatDate(key.created_at)}</span>
</div>
{key.last_used_at && (
<div className="flex items-center gap-1">
<Sparkles className="h-3 w-3" />
<span>Last used: {formatDate(key.last_used_at)}</span>
</div>
)}
</div>
</div>
</div>
<Button
size="sm"
variant="ghost"
onClick={() => handleDeleteKey(key.id)}
className="text-red-600 hover:text-red-700 hover:bg-red-50"
disabled={!key.is_active}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</motion.div>
))}
</div>
)}
</div>
{/* API Usage Guide */}
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2 }}
className="max-w-4xl mx-auto mt-12"
>
<div className="bg-white rounded-2xl border border-slate-200 p-8">
<div className="flex items-center gap-3 mb-6">
<div className="h-12 w-12 rounded-xl bg-indigo-50 flex items-center justify-center">
<Code className="h-6 w-6 text-indigo-600" />
</div>
<div>
<h2 className="text-xl font-bold text-slate-900">How to Use API Keys</h2>
<p className="text-sm text-slate-500">
Integrate document parsing into your external applications
</p>
</div>
</div>
<div className="space-y-6">
{/* Python Example */}
<div className="bg-slate-50 rounded-xl p-6 border border-slate-200">
<div className="flex items-center gap-2 mb-4">
<FileText className="h-5 w-5 text-slate-600" />
<h3 className="font-semibold text-slate-900">Python Example</h3>
</div>
<pre className="bg-slate-900 text-slate-100 rounded-lg p-4 overflow-x-auto text-sm">
<code>{`import requests
API_URL = "${API_BASE_URL}"
API_KEY = "sk_live_YOUR_API_KEY_HERE"
def extract_document(file_path, key_fields=None):
with open(file_path, 'rb') as f:
files = {'file': f}
data = {}
if key_fields:
data['key_fields'] = key_fields
response = requests.post(
f"{API_URL}/api/extract",
headers={"X-API-Key": API_KEY},
files=files,
data=data
)
return response.json()
# Usage
result = extract_document("invoice.pdf",
key_fields="Invoice Number,Invoice Date")
print(result)`}</code>
</pre>
<Button
size="sm"
variant="outline"
onClick={() => copyToClipboard(`import requests
API_URL = "${API_BASE_URL}"
API_KEY = "sk_live_YOUR_API_KEY_HERE"
def extract_document(file_path, key_fields=None):
with open(file_path, 'rb') as f:
files = {'file': f}
data = {}
if key_fields:
data['key_fields'] = key_fields
response = requests.post(
f"{API_URL}/api/extract",
headers={"X-API-Key": API_KEY},
files=files,
data=data
)
return response.json()
# Usage
result = extract_document("invoice.pdf",
key_fields="Invoice Number,Invoice Date")
print(result)`)}
className="mt-3"
>
<Copy className="h-3 w-3 mr-2" />
Copy Code
</Button>
</div>
{/* cURL Example */}
<div className="bg-slate-50 rounded-xl p-6 border border-slate-200">
<div className="flex items-center gap-2 mb-4">
<FileText className="h-5 w-5 text-slate-600" />
<h3 className="font-semibold text-slate-900">cURL Example</h3>
</div>
<pre className="bg-slate-900 text-slate-100 rounded-lg p-4 overflow-x-auto text-sm">
<code>{`curl -X POST ${API_BASE_URL}/api/extract \\
-H "X-API-Key: sk_live_YOUR_API_KEY_HERE" \\
-F "file=@document.pdf" \\
-F "key_fields=Invoice Number,Invoice Date,Total Amount"`}</code>
</pre>
<Button
size="sm"
variant="outline"
onClick={() => copyToClipboard(`curl -X POST ${API_BASE_URL}/api/extract \\
-H "X-API-Key: sk_live_YOUR_API_KEY_HERE" \\
-F "file=@document.pdf" \\
-F "key_fields=Invoice Number,Invoice Date,Total Amount"`)}
className="mt-3"
>
<Copy className="h-3 w-3 mr-2" />
Copy Code
</Button>
</div>
{/* JavaScript Example */}
<div className="bg-slate-50 rounded-xl p-6 border border-slate-200">
<div className="flex items-center gap-2 mb-4">
<FileText className="h-5 w-5 text-slate-600" />
<h3 className="font-semibold text-slate-900">JavaScript/Node.js Example</h3>
</div>
<pre className="bg-slate-900 text-slate-100 rounded-lg p-4 overflow-x-auto text-sm">
<code>{`const FormData = require('form-data');
const fs = require('fs');
const axios = require('axios');
const API_URL = '${API_BASE_URL}';
const API_KEY = 'sk_live_YOUR_API_KEY_HERE';
async function extractDocument(filePath, keyFields = null) {
const form = new FormData();
form.append('file', fs.createReadStream(filePath));
if (keyFields) {
form.append('key_fields', keyFields);
}
const response = await axios.post(
\`\${API_URL}/api/extract\`,
form,
{
headers: {
'X-API-Key': API_KEY,
...form.getHeaders()
}
}
);
return response.data;
}
// Usage
extractDocument('invoice.pdf', 'Invoice Number,Invoice Date')
.then(result => console.log(result));`}</code>
</pre>
<Button
size="sm"
variant="outline"
onClick={() => copyToClipboard(`const FormData = require('form-data');
const fs = require('fs');
const axios = require('axios');
const API_URL = '${API_BASE_URL}';
const API_KEY = 'sk_live_YOUR_API_KEY_HERE';
async function extractDocument(filePath, keyFields = null) {
const form = new FormData();
form.append('file', fs.createReadStream(filePath));
if (keyFields) {
form.append('key_fields', keyFields);
}
const response = await axios.post(
\`\${API_URL}/api/extract\`,
form,
{
headers: {
'X-API-Key': API_KEY,
...form.getHeaders()
}
}
);
return response.data;
}
// Usage
extractDocument('invoice.pdf', 'Invoice Number,Invoice Date')
.then(result => console.log(result));`)}
className="mt-3"
>
<Copy className="h-3 w-3 mr-2" />
Copy Code
</Button>
</div>
</div>
<div className="mt-6 p-4 bg-indigo-50 rounded-xl border border-indigo-200">
<div className="flex items-start gap-3">
<AlertCircle className="h-5 w-5 text-indigo-600 flex-shrink-0 mt-0.5" />
<div className="flex-1">
<h4 className="font-semibold text-indigo-900 mb-1">API Endpoint</h4>
<p className="text-sm text-indigo-700 mb-2">
<code className="bg-white px-2 py-1 rounded text-indigo-900">
POST {API_BASE_URL}/api/extract
</code>
</p>
<p className="text-xs text-indigo-600">
• Max file size: 4 MB • Supported formats: PDF, PNG, JPEG, TIFF
</p>
</div>
</div>
</div>
</div>
</motion.div>
</div>
{/* Create API Key Modal */}
<AnimatePresence>
{showCreateModal && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4"
onClick={() => !isCreating && setShowCreateModal(false)}
>
<motion.div
initial={{ scale: 0.95, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.95, opacity: 0 }}
onClick={(e) => e.stopPropagation()}
className="bg-white rounded-2xl p-6 max-w-md w-full shadow-2xl"
>
<h2 className="text-xl font-bold text-slate-900 mb-2">Create New API Key</h2>
<p className="text-sm text-slate-500 mb-6">
Give your API key a descriptive name to identify it later
</p>
<Input
placeholder="e.g., Production API, Test Environment"
value={newKeyName}
onChange={(e) => setNewKeyName(e.target.value)}
className="mb-6"
onKeyPress={(e) => e.key === "Enter" && handleCreateKey()}
/>
<div className="flex gap-3">
<Button
variant="outline"
onClick={() => {
setShowCreateModal(false);
setNewKeyName("");
}}
disabled={isCreating}
className="flex-1"
>
Cancel
</Button>
<Button
onClick={handleCreateKey}
disabled={isCreating || !newKeyName.trim()}
className="flex-1 bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-700 hover:to-violet-700"
>
{isCreating ? "Creating..." : "Create Key"}
</Button>
</div>
</motion.div>
</motion.div>
)}
</AnimatePresence>
</div>
);
}
|