Code examples

# Read Watch events in code

No Watch SDK is required. These examples read events using the address shown by Watch and an events:read key.

Use the built-in viewer if you only need to inspect events. For the examples below, an integrator should set WATCH\_URL and WATCH\_API\_KEY in a trusted local environment. WATCH\_URL excludes /viewer/ and a trailing slash. Keep the actual key out of source code. [Test the connection first](https://watch.tensordynamics.org/developers/quickstart?lang=en)

## Python 3: no additional packages

```
import json
import os
import urllib.request

class NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        return None

base = os.environ["WATCH_URL"].rstrip("/")
key = os.environ["WATCH_API_KEY"]
request = urllib.request.Request(
    base + "/v1/events?limit=10",
    headers={"Authorization": "Bearer " + key},
)
opener = urllib.request.build_opener(
    urllib.request.ProxyHandler({}), NoRedirect()
)
with opener.open(request, timeout=10) as response:
    result = json.load(response)
    print("HTTP", response.status, "events:", len(result["data"]))
```

## JavaScript: run on a trusted backend

This example needs a JavaScript runtime with fetch. Do not put the key in a public website’s frontend code.

```
export async function readEvents(baseUrl, apiKey) {
  const url = new URL(baseUrl);
  url.pathname = url.pathname.endsWith("/") ? url.pathname.slice(0, -1) : url.pathname;
  url.pathname += "/v1/events";
  url.search = "?limit=10";
  url.hash = "";
  const response = await fetch(url, {
    method: "GET",
    redirect: "error",
    signal: AbortSignal.timeout(10000),
    headers: { Authorization: "Bearer " + apiKey },
  });
  if (!response.ok) throw new Error("Watch HTTP " + response.status);
  return response.json();
}

const result = await readEvents(process.env.WATCH_URL, process.env.WATCH_API_KEY);
console.log("Events:", result.data.length);
```

## Windows PowerShell

```
$Headers = @{ Authorization = "Bearer $env:WATCH_API_KEY" }
(Invoke-WebRequest -UseBasicParsing -TimeoutSec 10 -MaximumRedirection 0 -Headers $Headers -Uri "$env:WATCH_URL/v1/events?limit=1").StatusCode
```

## Check the result

HTTP 200 or a successful JSON response confirms access. Zero events is still a successful read. Read result.data when your application needs the event contents. For 401, check the key; for 403, ask the administrator to check permissions and the allowed LAN.

## Generate a client from OpenAPI

You can also download public OpenAPI for your client generator. Set the generated client’s server URL to your Watch instance and use the event-read operations you need. [OpenAPI and full reference](https://watch.tensordynamics.org/developers/api-reference?lang=en)

[Handle pagination, errors, and retries](https://watch.tensordynamics.org/developers/api-behavior?lang=en)

---
Source: https://watch.tensordynamics.org/developers/examples
More Watch guides: https://watch.tensordynamics.org/llms.txt
