Toggle endpoint state (JavaScript)
The following code example toggles the state of an endpoint by ID:
1 function toggleStatus(endpointId) { 2 const baseUrl = 'cdn.emnify.net'; 3 const token = 'AUTH_TOKEN'; 4 const headers={ 5 'Accept':'application/json', 6 'Authorization': `Bearer ${token}` 7 } 8 9 fetch(`${baseUrl}api/v1/endpoint/${endpointId}`, { 10 method: 'GET', 11 headers: new Headers(headers) 12 }) 13 .then(response => response.json()) 14 .then(endpoint => { 15 16 // The endpoint status is retrieved as an object {id: 0, description: "Enabled"} 17 // and should be set as an object without a description 18 // Available statuses: 19 // 0 - Enabled 20 // 1 - Disabled 21 const status = endpoint.status.id == 1 ? { id: 0 } : { id: 1 }; 22 23 return fetch(`${baseUrl}api/v1/endpoint/${endpointId}`, { 24 method: 'PATCH', 25 headers: new Headers({ 26 'Content-Type':'application/json;charset=UTF-8', 27 ...headers 28 }), 29 body: JSON.stringify({ status }) 30 }) 31 }) 32 .then(console.log) 33 .catch(console.error) 34 }