PROVESID supports persistent API key storage, making it easy to configure your API keys once and use them automatically in future sessions.
from provesid import set_cas_api_key
# Set your API key once
set_cas_api_key("your-cas-api-key-here")from provesid import CASCommonChem
# No need to provide API key - it's automatically loaded!
cas_api = CASCommonChem()
# Use normally
result = cas_api.cas_to_detail("64-17-5") # Ethanol
print(result['name']) # "Ethanol"from provesid import show_config
show_config()Output:
PROVESID Configuration:
Config directory: C:\Users\username\AppData\Roaming\PROVESID
Config file: C:\Users\username\AppData\Roaming\PROVESID\config.json
Config exists: True
Configured services: cas
from provesid import remove_cas_api_key
remove_cas_api_key()from provesid import get_cas_api_key
api_key = get_cas_api_key()
if api_key:
print("API key is configured")
else:
print("No API key configured")When initializing CASCommonChem(), the system checks for API keys in this order:
- Direct parameter:
CASCommonChem(api_key="your-key") - File parameter:
CASCommonChem(api_key_file="path/to/key.txt") - Persistent config: Set via
set_cas_api_key("your-key") - Environment variables:
CCC_API_KEYorCAS_API_KEY
- Config directory:
%APPDATA%\PROVESID\ - Config file:
%APPDATA%\PROVESID\config.json
- Config directory:
~/.config/provesid/ - Config file:
~/.config/provesid/config.json
- The API key is stored in plain text in the configuration file
- The configuration file is stored in your user directory (not system-wide)
- File permissions are set to be readable only by your user account
- For enhanced security, consider using environment variables in production
If you were previously providing the API key manually:
Before:
cas_api = CASCommonChem(api_key="your-key")After (one-time setup):
# Run once to configure
from provesid import set_cas_api_key
set_cas_api_key("your-key")
# Then use anywhere without specifying the key
from provesid import CASCommonChem
cas_api = CASCommonChem() # Automatically uses stored keyIf you get this error, it means no API key was found. The error message shows all available options:
API key is required for CAS Common Chemistry API v2.0.
Options:
1. Provide api_key parameter: CASCommonChem(api_key='your-key')
2. Provide api_key_file parameter: CASCommonChem(api_key_file='path/to/key.txt')
3. Set persistent API key: from provesid.config import set_cas_api_key; set_cas_api_key('your-key')
4. Set environment variable: CCC_API_KEY or CAS_API_KEY
from provesid import show_config, get_cas_api_key
show_config() # Shows configuration status
key = get_cas_api_key() # Returns None if not configured
print(f"API key configured: {bool(key)}")To completely reset your configuration:
from provesid import remove_cas_api_key
remove_cas_api_key()Or manually delete the config file shown by show_config().
# One-time setup
from provesid import set_cas_api_key, show_config
# Configure your API key
set_cas_api_key("your-cas-api-key-from-cas")
# Verify configuration
show_config()
# Now use normally
from provesid import CASCommonChem
cas = CASCommonChem()
# Test with a simple compound
water = cas.name_to_detail("water")
if water['found']:
print(f"Water CAS RN: {water['rn']}")
print(f"Molecular formula: {water['molecularFormula']}")# Use stored API key
cas1 = CASCommonChem()
# Override with different API key for specific instance
cas2 = CASCommonChem(api_key="different-api-key")
# Use file-based key for another instance
cas3 = CASCommonChem(api_key_file="/path/to/another/key.txt")