keyCreate Api Key

Generate an API key for service authentication.

circle-info

When to use: When you need programmatic access for backend services, CI/CD pipelines, or automation scripts.

chevron-rightPrerequisiteshashtag
  • Completed Quickstart

  • A running PostgreSQL instance with GL IAM configured

5-Line Core

from gl_iam.providers.postgresql import PostgreSQLProvider, PostgreSQLConfig

config = PostgreSQLConfig(database_url="postgresql+asyncpg://...")
provider = PostgreSQLProvider(config)
api_key, plain_key = await provider.create_api_key(
    name="my-service",
    organization_id="default",
    scopes=["api:read", "api:write"],
)
# Store plain_key securely - only shown once!

Key Tiers

Tier
Use Case
Requirements

PLATFORM

System bootstrap

No org, no user

ORGANIZATION

Service keys

Requires org

PERSONAL

User automation

Requires org + user

Step-by-Step

1

Setup Provider

2

Create Organization Key

3

Store the Plain Key

4

Optional: Set Expiration

5

Expected Output

circle-check

Complete Example

Create create_api_key.py:

Run it:

Expected output:

Using with IAMGateway

For service-only authentication, use for_service_auth:

If you also need user authentication, use from_fullstack_provider instead:

Common Pitfalls

Pitfall
Solution

Not storing plain key

Store immediately - only shown once

Too broad scopes

Use minimum required scopes

No expiration

Set expiration for security


circle-info

Found an issue on this page? Report it on our feedback formarrow-up-right.

Last updated

Was this helpful?