---
title: "Dynamic Client Registration"
description: "How to register OAuth clients dynamically using the KeyCard OAuth SDK"
---

# Dynamic Client Registration

This example demonstrates how to use OAuth 2.0 Dynamic Client Registration (RFC 7591) to register a new client with the authorization server.

## Basic Registration

```python
from keycardai.oauth import Client

with Client("https://your-keycard-zone.com") as client:
    response = client.register_client(client_name="Test Client")
    
    print(f"Client registered: {response.client_id}")
    return response
```

```python
from keycardai.oauth import AsyncClient, ClientRegistrationRequest

async def register_client():
    async with AsyncClient("https://your-keycard-zone.com") as client:
        # Register the client
        response = await client.register_client(client_name="My Application",
            redirect_uris=["https://myapp.com/callback"],
            grant_types=["authorization_code", "refresh_token"],
            response_types=["code"],
            scope="openid profile email")
        
        print(f"Client ID: {response.client_id}")
        print(f"Client Secret: {response.client_secret}")
        print(f"Registration Access Token: {response.registration_access_token}")
        
        return response

# Run the registration
import asyncio
client_info = asyncio.run(register_client())
```

## Advanced Registration with Custom Metadata

```python
from keycardai.oauth import AsyncClient, ClientRegistrationRequest, TokenEndpointAuthMethod

async def register_advanced_client():
    async with AsyncClient("https://your-keycard-zone.com") as client:
        registration_request = ClientRegistrationRequest(
            client_name="Advanced OAuth Client",
            redirect_uris=[
                "https://myapp.com/callback",
                "https://myapp.com/silent-callback"
            ],
            grant_types=["authorization_code", "refresh_token", "client_credentials"],
            response_types=["code"],
            scope="openid profile email api:read api:write",
            token_endpoint_auth_method=TokenEndpointAuthMethod.CLIENT_SECRET_POST,
            application_type="web",
            contacts=["admin@myapp.com"],
            logo_uri="https://myapp.com/logo.png",
            policy_uri="https://myapp.com/privacy",
            tos_uri="https://myapp.com/terms"
        )
        
        response = await client.register_client(registration_request)
        
        # Store these securely!
        print("=== Client Registration Successful ===")
        print(f"Client ID: {response.client_id}")
        print(f"Client Secret: {response.client_secret}")
        print(f"Client ID Issued At: {response.client_id_issued_at}")
        print(f"Client Secret Expires At: {response.client_secret_expires_at}")
        
        return response

# Run the advanced registration
import asyncio
client_info = asyncio.run(register_advanced_client())
```

## Error Handling

```python
from keycardai.oauth import AsyncClient, ClientRegistrationRequest, OAuthError

async def register_with_error_handling():
    async with AsyncClient("https://your-keycard-zone.com") as client:
        try:
            registration_request = ClientRegistrationRequest(
                client_name="Test Client",
                redirect_uris=["https://myapp.com/callback"],
                grant_types=["authorization_code"],
                response_types=["code"],
                scope="openid profile"
            )
            
            response = await client.register_client(registration_request)
            print("Registration successful!")
            return response
            
        except OAuthError as e:
            print(f"OAuth error: {e}")
            # Handle specific OAuth errors
        except Exception as e:
            print(f"Unexpected error: {e}")
            # Handle other errors
            
# Run with error handling
import asyncio
asyncio.run(register_with_error_handling())
```
For token exchange examples, see the [Token Exchange guide](/examples/token-exchange).
