> This page is for Developers.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.iot.telia.lt/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.iot.telia.lt/_mcp/server.

# Toggle endpoint state (JavaScript)

> JavaScript code example to toggle endpoint status between enabled and disabled states using the API.

The following code example toggles the state of an endpoint by ID:

```javascript maxLines=0
function toggleStatus(endpointId) {
    const baseUrl = 'iot.telia.lt';
    const token = 'AUTH_TOKEN';
    const headers={
        'Accept':'application/json',
        'Authorization': `Bearer ${token}`
    }

    fetch(`${baseUrl}api/v1/endpoint/${endpointId}`, {
        method: 'GET',
        headers: new Headers(headers)
    })
        .then(response => response.json())
        .then(endpoint => {

            // The endpoint status is retrieved as an object {id: 0, description: "Enabled"}
            // and should be set as an object without a description
            // Available statuses:
            // 0 - Enabled
            // 1 - Disabled
            const status = endpoint.status.id == 1 ? { id: 0 } : { id: 1 };

            return fetch(`${baseUrl}api/v1/endpoint/${endpointId}`, {
                method: 'PATCH',
                headers: new Headers({
                    'Content-Type':'application/json;charset=UTF-8',
                    ...headers
                }),
                body: JSON.stringify({ status })
            })
        })
        .then(console.log)
        .catch(console.error)
}
```