| 1 | import time |
| 2 | from paygent_sdk import Client |
| 3 | from paygent_sdk.models import CustomerCreateOrGetRequest |
| 4 | |
| 5 | paygent = Client("your-paygent-api-key") |
| 6 | session_id = f"voice_call_{int(time.time() * 1000)}" |
| 7 | customer_id = "your-customer-id" |
| 8 | |
| 9 | # 1. Ensure Customer exists (Automatic Onboarding) |
| 10 | paygent.create_or_get_customer(CustomerCreateOrGetRequest( |
| 11 | name="Customer Name", |
| 12 | external_id=customer_id |
| 13 | )) |
| 14 | |
| 15 | # 2. Initialize Voice Session |
| 16 | paygent.initialize_voice_session( |
| 17 | session_id=session_id, |
| 18 | agent_id="your-external-agent-id", |
| 19 | customer_id=customer_id |
| 20 | ) |
| 21 | |
| 22 | # 3. Handle Realtime Events |
| 23 | async def _on_event(self, event: RealtimeSessionEvent) -> None: |
| 24 | if event.type == "audio": |
| 25 | ... |
| 26 | elif event.type == "audio_interrupted": |
| 27 | ... |
| 28 | elif event.type == "raw_model_event": |
| 29 | # Direct tracking from raw server events |
| 30 | # The SDK automatically extracts usage from 'response.done' |
| 31 | paygent.track_openai_sts( |
| 32 | usage_metadata=event.data, |
| 33 | session_id=self.session_id, |
| 34 | model="gpt-realtime-2025-08-28" |
| 35 | ) |
| 36 | |
| 37 | # 4. On Session End |
| 38 | duration_minutes = (time.time() - start_time) / 60.0 |
| 39 | paygent.set_voice_indicator(session_id, indicator_name, duration_minutes) |