Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/python/webhooks/create_webhook_subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Usage: uv run python webhooks/create_webhook_subscription.py

import asyncio

from cdp import CdpClient
from cdp.webhook_types import CreateWebhookSubscriptionOptions
from dotenv import load_dotenv

load_dotenv()


async def main():
async with CdpClient() as cdp:
subscription = await cdp.webhooks.create_subscription(
CreateWebhookSubscriptionOptions(
description="Monitor wallet transactions",
event_types=[
"wallet.transaction.pending",
"wallet.transaction.confirmed",
"wallet.transaction.failed",
],
target_url="https://example.com/webhook",
)
)

print(f"Subscription ID: {subscription.subscription_id}")
print(f"Event Types: {subscription.event_types}")
print(f"Target URL: {subscription.target_url}")
print(f"Enabled: {subscription.is_enabled}")


asyncio.run(main())
23 changes: 23 additions & 0 deletions examples/typescript/webhooks/createWebhookSubscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Usage: pnpm tsx webhooks/createWebhookSubscription.ts

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";

const cdp = new CdpClient();

const subscription = await cdp.webhooks.createSubscription({
description: "Monitor wallet transactions",
eventTypes: [
"wallet.transaction.pending",
"wallet.transaction.confirmed",
"wallet.transaction.failed",
],
targetUrl: "https://example.com/webhook",
isEnabled: true,
});

console.log("Subscription ID:", subscription.subscriptionId);
console.log("Event types:", subscription.eventTypes);
console.log("Target URL:", subscription.targetUrl);
console.log("Enabled:", subscription.isEnabled);
console.log("Created at:", subscription.createdAt);
6 changes: 6 additions & 0 deletions python/cdp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from cdp.to_evm_delegated_account import to_evm_delegated_account
from cdp.update_account_types import UpdateAccountOptions
from cdp.utils import parse_units
from cdp.webhook_types import (
CreateWebhookSubscriptionOptions,
CreateWebhookSubscriptionResult,
)

__all__ = [
"CdpClient",
Expand All @@ -39,6 +43,8 @@
"TransactionRequestEIP1559",
"to_evm_delegated_account",
"UpdateAccountOptions",
"CreateWebhookSubscriptionOptions",
"CreateWebhookSubscriptionResult",
"__version__",
"parse_units",
]
3 changes: 3 additions & 0 deletions python/cdp/actions/webhooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from cdp.actions.webhooks.create_webhook_subscription import create_webhook_subscription

__all__ = ["create_webhook_subscription"]
46 changes: 46 additions & 0 deletions python/cdp/actions/webhooks/create_webhook_subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from cdp.openapi_client.api.webhooks_api import WebhooksApi
from cdp.openapi_client.models.webhook_subscription_request import WebhookSubscriptionRequest
from cdp.openapi_client.models.webhook_target import WebhookTarget
from cdp.webhook_types import (
CreateWebhookSubscriptionOptions,
CreateWebhookSubscriptionResult,
)


async def create_webhook_subscription(
webhooks_api: WebhooksApi,
options: CreateWebhookSubscriptionOptions,
) -> CreateWebhookSubscriptionResult:
"""Create a webhook subscription for wallet transaction events.

Args:
webhooks_api (WebhooksApi): The webhooks API client.
options (CreateWebhookSubscriptionOptions): The options for creating the webhook subscription.

Returns:
CreateWebhookSubscriptionResult: The created webhook subscription.

"""
response = await webhooks_api.create_webhook_subscription(
webhook_subscription_request=WebhookSubscriptionRequest(
description=options.description,
event_types=list(options.event_types),
target=WebhookTarget(
url=options.target_url,
headers=options.target_headers,
),
is_enabled=options.is_enabled if options.is_enabled is not None else True,
metadata=options.metadata,
),
)

return CreateWebhookSubscriptionResult(
subscription_id=response.subscription_id,
description=response.description,
event_types=list(response.event_types),
target_url=response.target.url,
target_headers=response.target.headers,
is_enabled=response.is_enabled,
secret=response.secret,
created_at=str(response.created_at),
)
18 changes: 18 additions & 0 deletions python/cdp/api_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cdp.openapi_client.api.policy_engine_api import PolicyEngineApi
from cdp.openapi_client.api.solana_accounts_api import SolanaAccountsApi
from cdp.openapi_client.api.solana_token_balances_api import SolanaTokenBalancesApi
from cdp.openapi_client.api.webhooks_api import WebhooksApi
from cdp.openapi_client.cdp_api_client import CdpApiClient


Expand Down Expand Up @@ -49,6 +50,7 @@ def __init__(self, cdp_client: CdpApiClient) -> None:
self._solana_token_balances: SolanaTokenBalancesApi | None = None
self._policies: PolicyEngineApi | None = None
self._end_user: EndUserAccountsApi | None = None
self._webhooks: WebhooksApi | None = None
self._closed = False

def _check_closed(self) -> None:
Expand Down Expand Up @@ -216,6 +218,22 @@ def end_user(self) -> EndUserAccountsApi:
self._end_user = EndUserAccountsApi(api_client=self._cdp_client)
return self._end_user

@property
def webhooks(self) -> WebhooksApi:
"""Get the WebhooksApi client instance.

Returns:
WebhooksApi: The WebhooksApi client instance.

Note:
This property lazily initializes the WebhooksApi client on first access.

"""
self._check_closed()
if self._webhooks is None:
self._webhooks = WebhooksApi(api_client=self._cdp_client)
return self._webhooks

async def close(self):
"""Close the CDP client asynchronously."""
await self._cdp_client.close()
Expand Down
8 changes: 8 additions & 0 deletions python/cdp/cdp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from cdp.openapi_client.cdp_api_client import CdpApiClient
from cdp.policies_client import PoliciesClient
from cdp.solana_client import SolanaClient
from cdp.webhooks_client import WebhooksClient


class CdpClient:
Expand Down Expand Up @@ -93,6 +94,7 @@ def __init__(
self._solana = SolanaClient(self.api_clients)
self._policies = PoliciesClient(self.api_clients)
self._end_user = EndUserClient(self.api_clients)
self._webhooks = WebhooksClient(self.api_clients)
self._closed = False

if (
Expand Down Expand Up @@ -125,6 +127,12 @@ def end_user(self) -> EndUserClient:
self._check_closed()
return self._end_user

@property
def webhooks(self) -> WebhooksClient:
"""Get the WebhooksClient instance."""
self._check_closed()
return self._webhooks

def _check_closed(self) -> None:
"""Check if the client has been closed and raise an appropriate error."""
if self._closed:
Expand Down
Loading
Loading