Skip to content

Conversation

@blackmad-cradle
Copy link
Contributor

This addresses the issues in #508

Adds full API key management to the SDK based on PR #508 with review feedback addressed:

  • Organizations: create_api_key and list_api_keys with pagination
  • ApiKeys: delete_api_key
  • Removed organization_id from list filters (it's a URL path param, not a query param) per reviewer feedback
  • Used functools.partial for list_method to properly support auto-pagination without sending organization_id as query param

Description

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

Adds full API key management to the SDK based on PR workos#508 with review
feedback addressed:
- Organizations: create_api_key and list_api_keys with pagination
- ApiKeys: delete_api_key
- Removed organization_id from list filters (it's a URL path param,
  not a query param) per reviewer feedback
- Used functools.partial for list_method to properly support
  auto-pagination without sending organization_id as query param

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@blackmad-cradle blackmad-cradle requested a review from a team as a code owner February 12, 2026 18:15
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 12, 2026

Greptile Overview

Greptile Summary

This PR adds comprehensive API key management functionality to the WorkOS Python SDK, including create, list, and delete operations. The implementation follows the established SDK patterns with both sync and async client support.

Key Changes:

  • Added create_api_key() method to Organizations module that returns ApiKeyWithValue containing the full API key (only available at creation time)
  • Added list_api_keys() method with proper auto-pagination support using functools.partial to bind organization_id
  • Added delete_api_key() method to ApiKeys module for deleting API keys by ID
  • Created ApiKeyWithValue type extending ApiKey with the sensitive value field
  • Comprehensive test coverage including auto-pagination tests

Notable Implementation Details:

  • Correctly uses functools.partial(self.list_api_keys, organization_id) for the list_method to ensure auto-pagination works properly, since organization_id is a path parameter (not a query parameter)
  • The ApiKeyWithValue.value field contains the full API key secret, which is only returned once during creation
  • All methods properly implemented for both sync (Organizations, ApiKeys) and async (AsyncOrganizations, AsyncApiKeys) variants
  • Tests verify that organization_id is not passed as a query parameter during pagination

Confidence Score: 5/5

  • This PR is safe to merge with no issues found
  • The implementation correctly follows established SDK patterns, properly handles sensitive data without logging, includes comprehensive test coverage (including critical auto-pagination tests), and addresses previous review feedback about using functools.partial for path parameters. All code adheres to type safety requirements and matches existing code style.
  • No files require special attention

Important Files Changed

Filename Overview
src/workos/api_keys.py Added delete_api_key method for both sync and async clients with proper DELETE request implementation
src/workos/organizations.py Added create_api_key and list_api_keys methods with correct use of functools.partial for auto-pagination support
src/workos/types/api_keys/api_keys.py Added ApiKeyWithValue class that extends ApiKey with sensitive value field for creation responses
tests/test_organizations.py Added thorough tests for API key creation and listing, including auto-pagination test to verify functools.partial fix

Sequence Diagram

sequenceDiagram
    participant Client
    participant Organizations
    participant HTTP Client
    participant WorkOS API

    Note over Client,WorkOS API: Create API Key Flow
    Client->>Organizations: create_api_key(org_id, name, permissions)
    Organizations->>HTTP Client: POST /organizations/{org_id}/api_keys
    HTTP Client->>WorkOS API: POST request with name & permissions
    WorkOS API-->>HTTP Client: 201 Created with ApiKeyWithValue
    HTTP Client-->>Organizations: response
    Organizations-->>Client: ApiKeyWithValue (includes value field)

    Note over Client,WorkOS API: List API Keys Flow
    Client->>Organizations: list_api_keys(org_id, limit, before, after, order)
    Organizations->>HTTP Client: GET /organizations/{org_id}/api_keys
    HTTP Client->>WorkOS API: GET request with pagination params
    WorkOS API-->>HTTP Client: 200 OK with list page
    HTTP Client-->>Organizations: response
    Organizations-->>Client: WorkOSListResource with partial(list_api_keys, org_id)

    Note over Client,WorkOS API: Auto-Pagination Flow
    Client->>Organizations: iterate over WorkOSListResource
    Organizations->>Organizations: partial(list_api_keys, org_id) called with after cursor
    Organizations->>HTTP Client: GET /organizations/{org_id}/api_keys?after=cursor
    HTTP Client->>WorkOS API: GET request with after param
    WorkOS API-->>HTTP Client: Next page
    HTTP Client-->>Organizations: response
    Organizations-->>Client: Next batch of API keys

    Note over Client,WorkOS API: Delete API Key Flow
    Client->>ApiKeys: delete_api_key(api_key_id)
    ApiKeys->>HTTP Client: DELETE /api_keys/{api_key_id}
    HTTP Client->>WorkOS API: DELETE request
    WorkOS API-->>HTTP Client: 204 No Content
    HTTP Client-->>ApiKeys: empty response
    ApiKeys-->>Client: None
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@gjtorikian gjtorikian mentioned this pull request Feb 12, 2026
@gjtorikian gjtorikian merged commit c162fb0 into workos:main Feb 12, 2026
11 checks passed
@greptile-apps greptile-apps bot mentioned this pull request Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants