key_manager
key_manager
¶
SuperCell Developer Portal Key Manager¶
Automates API key management for the Clash of Clans developer portal.
Handles login, IP detection, key creation/revocation, and automatic IP-based key rotation. Ported to httpx from the aiohttp reference implementation (coc.py's http.py / marsidev/get-sc-key).
Developer Portal Internal API Endpoints
Base URL: https://developer.clashofclans.com/api
POST /login - Authenticate with email/password (session cookie) POST /apikey/list - List all keys for the authenticated account POST /apikey/create - Create a new API key POST /apikey/revoke - Delete/revoke an existing API key
KeyManagerError
¶
Bases: Exception
Base exception for key manager errors.
InvalidCredentials
¶
Bases: KeyManagerError
Raised when email/password login fails.
KeyLimitReached
¶
Bases: KeyManagerError
Raised when the 10 key per account limit is hit.
SyncKeyManager
¶
SyncKeyManager(
email: str,
password: str,
key_name: str = "cocapi_auto",
key_count: int = 1,
key_description: str = "Auto-generated by cocapi KeyManager",
key_scopes: str = "clash",
persist_keys: bool = False,
key_storage_path: str | None = None,
)
Synchronous key manager for the SuperCell developer portal.
Handles login, IP detection, and API key lifecycle management using httpx.Client with cookie persistence.
Usage::
with SyncKeyManager("email", "password") as km:
tokens = km.manage_keys()
# Use tokens[0] with CocApi
Or via CocApi integration::
api = CocApi.from_credentials("email@example.com", "password")
Source code in cocapi/key_manager.py
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 | |
close
¶
close() -> None
Close the underlying HTTP client.
Source code in cocapi/key_manager.py
292 293 294 | |
manage_keys
¶
manage_keys() -> list[str]
Main orchestration: ensure we have valid keys for the current IP.
If persist_keys is enabled, checks the local cache first. When the cached IP matches the current IP, returns cached tokens immediately without contacting the developer portal.
- (Optional) Check local cache
- Login to developer portal
- Detect current public IP
- List existing keys
- Revoke stale keys (wrong IP), keep valid ones
- Create new keys if needed
- (Optional) Save tokens to local cache
- Return list of usable API token strings
Source code in cocapi/key_manager.py
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 | |
refresh_keys
¶
refresh_keys() -> list[str]
Re-detect IP and re-initialize keys.
Call this when the API returns accessDenied.invalidIp.
Source code in cocapi/key_manager.py
533 534 535 536 537 538 539 540 541 542 543 544 | |
AsyncKeyManager
¶
AsyncKeyManager(
email: str,
password: str,
key_name: str = "cocapi_auto",
key_count: int = 1,
key_description: str = "Auto-generated by cocapi KeyManager",
key_scopes: str = "clash",
persist_keys: bool = False,
key_storage_path: str | None = None,
)
Async key manager for the SuperCell developer portal.
Same functionality as SyncKeyManager but using httpx.AsyncClient.
Usage::
async with AsyncKeyManager("email", "password") as km:
tokens = await km.manage_keys()
Source code in cocapi/key_manager.py
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 | |
close
async
¶
close() -> None
Close the underlying async HTTP client.
Source code in cocapi/key_manager.py
597 598 599 600 601 | |
manage_keys
async
¶
manage_keys() -> list[str]
Main orchestration: ensure we have valid keys for the current IP. Same logic as SyncKeyManager.manage_keys() but async.
Source code in cocapi/key_manager.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 | |
refresh_keys
async
¶
refresh_keys() -> list[str]
Re-detect IP and re-initialize keys.
Source code in cocapi/key_manager.py
818 819 820 821 822 823 824 825 | |