Spaces:
Runtime error
Runtime error
import secrets | |
import json | |
import os | |
API_KEYS_FILE = 'api_keys.json' | |
def generate_api_key(): | |
"""Generate a secure random API key.""" | |
return secrets.token_hex(32) | |
def save_api_key(user_id, api_key): | |
"""Save the API key associated with a user.""" | |
if os.path.exists(API_KEYS_FILE): | |
with open(API_KEYS_FILE, 'r') as f: | |
api_keys = json.load(f) | |
else: | |
api_keys = {} | |
api_keys[user_id] = api_key | |
with open(API_KEYS_FILE, 'w') as f: | |
json.dump(api_keys, f) | |
def generate_and_save_api_keys(num_keys=15): | |
"""Generate and save a specified number of API keys.""" | |
for i in range(1, num_keys + 1): | |
user_id = f"user_{i}" | |
api_key = generate_api_key() | |
save_api_key(user_id, api_key) | |
print(f"API Key for {user_id}: {api_key}") | |
if __name__ == "__main__": | |
generate_and_save_api_keys(15) | |