Welcome to the Watsh WebSocket Integration Guide! This guide will walk you through the process of integrating Watsh's WebSocket API into your Python application. The WebSocket API allows you to receive real-time updates and notifications from your Watsh environment.
Prerequisites
Before you begin, make sure you have the following:
Python installed on your machine.
Access to a Watsh account with the necessary permissions.
An active Watsh project with WebSocket support enabled.
Step 1: Obtain API Key
To use the WebSocket API, you need an API key. Follow these steps to obtain your API key:
Log in to your Watsh account at Watsh Dashboard.
Request a login link if you haven't logged in before.
Navigate to the "Settings" section.
Under "Access Token," set an expiry for your token and click "Create."
Copy the generated API key.
Step 2: Install Dependencies
Make sure to install the required Python library for WebSocket communication:
pip install websockets
Step 3: Integrate the WebSocket into Your Python Script
Copy the provided Python script and customize the variables ACCESS_TOKEN, PROJECT_ID, ENVIRONMENT_ID, and BRANCH_ID with your specific values.
import asyncio
import json
from websockets.client import connect
HOST = 'wss://api.watsh.io'
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
PROJECT_ID = 'YOUR_PROJECT_ID'
ENVIRONMENT_ID = 'YOUR_ENVIRONMENT_ID'
BRANCH_ID = 'YOUR_BRANCH_ID'
WS_HOST = f"{HOST}/v1/{PROJECT_ID}/{ENVIRONMENT_ID}/{BRANCH_ID}"
async def main():
async with connect(WS_HOST + "?token=" + ACCESS_TOKEN) as websocket:
async for rcv in websocket:
message = json.loads(rcv)
print(message)
if __name__ == "__main__":
asyncio.run(main())
Replace "YOUR_ACCESS_TOKEN," "YOUR_PROJECT_ID," "YOUR_ENVIRONMENT_ID," and "YOUR_BRANCH_ID" with your actual values.
Step 4: Run Your Python Script
Execute your Python script to establish a WebSocket connection with Watsh. You should start receiving real-time updates from your Watsh environment.
python your_script_name.py
Congratulations! You have successfully integrated Watsh's WebSocket API into your Python application. Feel free to explore the WebSocket messages and customize the integration according to your needs.
For more detailed information about the available WebSocket events and payloads, refer to the Watsh WebSocket API Documentation.
If you encounter any issues or have questions, please reach out to our support team.
Happy coding!
