Resources

API Keys

Overview

API keys provide programmatic access to the LevelFour API. Each key has a name, an optional scope (read or read-write), and an optional expiration date. Keys use the prefix l4_live_ for production and l4_test_ for test environments.

Methods

List Keys

Returns all API keys for your organization.

keys = client.api_keys.list()

Create a Key

Creates a new API key with a name and optional scope and expiration.

from datetime import datetime

key = client.api_keys.create(
    name="CI Pipeline",
    scope="read",
    expires_at=datetime(2025, 12, 31, 23, 59, 59),
)
Store the returned key value immediately. It will not be shown again.

Parameters

ParameterTypeDescription
namestringDisplay name for the key
scopestring?read or read-write
expires_atdatetime?Optional expiration timestamp

Revoke a Key

Permanently invalidates a key. This cannot be undone.

client.api_keys.revoke("key_123")

Rotate a Key

Generates a new secret for an existing key ID. The old secret is immediately invalidated.

rotated = client.api_keys.rotate("key_123")

Example: Rotate All Keys

from levelfour import LevelFour

client = LevelFour()

keys = client.api_keys.list()
for key in keys.data:
    rotated = client.api_keys.rotate(key.id)
    print(f"Rotated {key.name}: {rotated}")