|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Nylas SDK Example: Using Metadata Fields with Drafts and Messages |
| 4 | +
|
| 5 | +This example demonstrates how to use metadata fields when creating drafts |
| 6 | +and sending messages using the Nylas Python SDK. |
| 7 | +
|
| 8 | +Required Environment Variables: |
| 9 | + NYLAS_API_KEY: Your Nylas API key |
| 10 | + NYLAS_GRANT_ID: Your Nylas grant ID |
| 11 | + TEST_EMAIL: Email address for sending test messages (optional) |
| 12 | +
|
| 13 | +Usage: |
| 14 | + First, install the SDK in development mode: |
| 15 | + cd /path/to/nylas-python |
| 16 | + pip install -e . |
| 17 | +
|
| 18 | + Then set environment variables and run: |
| 19 | + export NYLAS_API_KEY="your_api_key" |
| 20 | + export NYLAS_GRANT_ID="your_grant_id" |
| 21 | + export TEST_EMAIL="recipient@example.com" |
| 22 | + python examples/metadata_field_demo/metadata_example.py |
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | +import sys |
| 27 | +from typing import Dict, Any, Optional |
| 28 | + |
| 29 | +# Import from local nylas package |
| 30 | +from nylas import Client |
| 31 | +from nylas.models.errors import NylasApiError |
| 32 | + |
| 33 | + |
| 34 | +def get_env_or_exit(var_name: str, required: bool = True) -> Optional[str]: |
| 35 | + """Get an environment variable or exit if required and not found.""" |
| 36 | + value = os.getenv(var_name) |
| 37 | + if required and not value: |
| 38 | + print(f"Error: {var_name} environment variable is required") |
| 39 | + sys.exit(1) |
| 40 | + return value |
| 41 | + |
| 42 | + |
| 43 | +def create_draft_with_metadata( |
| 44 | + client: Client, grant_id: str, metadata: Dict[str, Any], recipient: str |
| 45 | +) -> str: |
| 46 | + """Create a draft message with metadata fields.""" |
| 47 | + try: |
| 48 | + draft_request = { |
| 49 | + "subject": "Test Draft with Metadata", |
| 50 | + "to": [{"email": recipient}], |
| 51 | + "body": "This is a test draft with metadata fields.", |
| 52 | + "metadata": metadata |
| 53 | + } |
| 54 | + |
| 55 | + draft, request_id = client.drafts.create( |
| 56 | + identifier=grant_id, |
| 57 | + request_body=draft_request |
| 58 | + ) |
| 59 | + print(f"✓ Created draft with ID: {draft.id}") |
| 60 | + print(f" Request ID: {request_id}") |
| 61 | + return draft.id |
| 62 | + except NylasApiError as e: |
| 63 | + print(f"✗ Failed to create draft: {e}") |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | + |
| 67 | +def send_message_with_metadata( |
| 68 | + client: Client, grant_id: str, metadata: Dict[str, Any], recipient: str |
| 69 | +) -> str: |
| 70 | + """Send a message directly with metadata fields.""" |
| 71 | + try: |
| 72 | + message_request = { |
| 73 | + "subject": "Test Message with Metadata", |
| 74 | + "to": [{"email": recipient}], |
| 75 | + "body": "This is a test message with metadata fields.", |
| 76 | + "metadata": metadata |
| 77 | + } |
| 78 | + |
| 79 | + message, request_id = client.messages.send( |
| 80 | + identifier=grant_id, |
| 81 | + request_body=message_request |
| 82 | + ) |
| 83 | + print(f"✓ Sent message with ID: {message.id}") |
| 84 | + print(f" Request ID: {request_id}") |
| 85 | + |
| 86 | + return message.id |
| 87 | + except NylasApiError as e: |
| 88 | + print(f"✗ Failed to send message: {e}") |
| 89 | + sys.exit(1) |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + """Main function demonstrating metadata field usage.""" |
| 94 | + # Get required environment variables |
| 95 | + api_key = get_env_or_exit("NYLAS_API_KEY") |
| 96 | + grant_id = get_env_or_exit("NYLAS_GRANT_ID") |
| 97 | + recipient = get_env_or_exit("TEST_EMAIL", required=False) or "recipient@example.com" |
| 98 | + |
| 99 | + # Initialize Nylas client |
| 100 | + client = Client( |
| 101 | + api_key=api_key, |
| 102 | + ) |
| 103 | + |
| 104 | + # Example metadata |
| 105 | + metadata = { |
| 106 | + "campaign_id": "example-123", |
| 107 | + "user_id": "user-456", |
| 108 | + "custom_field": "test-value" |
| 109 | + } |
| 110 | + |
| 111 | + print("\nDemonstrating Metadata Field Usage") |
| 112 | + print("=================================") |
| 113 | + |
| 114 | + # Create a draft with metadata |
| 115 | + print("\n1. Creating draft with metadata...") |
| 116 | + draft_id = create_draft_with_metadata(client, grant_id, metadata, recipient) |
| 117 | + |
| 118 | + # Send a message with metadata |
| 119 | + print("\n2. Sending message with metadata...") |
| 120 | + message_id = send_message_with_metadata(client, grant_id, metadata, recipient) |
| 121 | + |
| 122 | + print("\nExample completed successfully!") |
| 123 | + |
| 124 | + # Get the draft and message to demonstrate metadata retrieval |
| 125 | + draft = client.drafts.find(identifier=grant_id, draft_id=draft_id) |
| 126 | + message = client.messages.find(identifier=grant_id, message_id=message_id) |
| 127 | + |
| 128 | + print("\nRetrieved Draft Metadata:") |
| 129 | + print("-------------------------") |
| 130 | + print(draft.data) |
| 131 | + |
| 132 | + print("\nRetrieved Message Metadata:") |
| 133 | + print("---------------------------") |
| 134 | + print(message.data) |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments