Examples

Complete Example

A complete example showing all major features:

from nexacon import NexaconClient

# Initialize client
client = NexaconClient(
    api_key="your_api_key",
    secret_key="your_secret_key",
    username="+255788811191"
)

try:
    # Send message
    result = client.messaging.send(
        to="+255788811191",
        message="Hello from Nexacon SDK!"
    )
    print(f"Message sent: {result}")

    # Get contacts
    contacts = client.messaging.get_contacts()
    print(f"Contacts: {len(contacts)}")

    # Initiate call
    call = client.calls.initiate_call(
        to="+255788811191",
        call_type="video",
        room="my-room"
    )
    print(f"Call initiated: {call}")

    # Register device
    device = client.devices.register(
        fcm_token="device_fcm_token",
        platform="android"
    )
    print(f"Device registered: {device}")

    # Create room
    room = client.rooms.create(
        title="My Group",
        description="Group chat"
    )
    print(f"Room created: {room}")

    # Check presence
    presence = client.presence.get(user="user_nxid")
    print(f"Presence: {presence}")

except Exception as e:
    print(f"Error: {e}")
finally:
    client.close()

Error Handling

Always handle errors gracefully:

from nexacon import NexaconClient
from nexacon.exceptions import (
    NexaconError,
    AuthenticationError,
    APIError,
    ValidationError
)

client = NexaconClient(
    api_key="your_api_key",
    secret_key="your_secret_key",
    username="+255788811191"
)

try:
    result = client.messaging.send(
        to="+255788811191",
        message="Hello!"
    )
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
except ValidationError as e:
    print(f"Validation error: {e}")
except APIError as e:
    print(f"API error: {e}")
except NexaconError as e:
    print(f"Nexacon error: {e}")
finally:
    client.close()