UNPKG

fritzbox-api

Version:

Straightforward, lightweight and extendable Node.js library to communicate with FRITZ!Box devices

85 lines (69 loc) 2.41 kB
// Copyright (c) 2026, Thorsten A. Weintz. All rights reserved. // Licensed under the MIT license. See LICENSE in the project root for license information. import FritzBoxApi, { url, setCredentials } from './config.mjs'; /** * Important Notice: * * Ensure that dialing assistance is enabled and an outgoing device is selected * on the FRITZ!Box before using this script. * * This is necessary for making calls through the FRITZ!Box API, as the system needs * to know which device to use for outgoing calls and that dialing assistance is active. */ /** * Writes a message to the console. */ const log = console.log; /** * Logs the HTTP status of a request. * * @param {number} num Request identifier. * @param {string} action HTTP method or action description. * @param {object} response HTTP response object. * @param {number} response.status HTTP status code of the response. * @param {string} response.statusText Description corresponding to the HTTP status. */ const logHttpStatus = (num, action, response) => { const { status, statusText } = response; log(`${num}. ${action}: HTTP status ${status} ${statusText}`); }; /** * Initializes new instance of @see FritzBoxApi and sets options. */ const fritzBoxApi = new FritzBoxApi({ url }); /** * Sets the credentials used for authentication against the FRITZ!Box. */ setCredentials(fritzBoxApi); /** * The device identifier for the call (e.g., phone number, extension, or device ID). */ const number = '**610'; /** * Initiates a call to a device using its identifier through the FRITZ!Box API. */ let response = await fritzBoxApi.callDevice(number); /** * Checks if the response status is not 200 (OK). * If it's an error, logs the error message and terminates the script. */ if (response.status !== 200) { log(`Error: ${response.error}`); process.exit(1); } /** * Prints call state of @see FritzBoxApi to stdout. */ logHttpStatus(1, 'Call ' + number, response); /** * Waits for 5 seconds before proceeding to simulate time between actions. */ await new Promise(resolve => setTimeout(resolve, 5000)); /** * Hangs up the current call through the FRITZ!Box API. */ response = await fritzBoxApi.hangup(); /** * Prints hangup state of @see FritzBoxApi to stdout. */ logHttpStatus(2, 'Hang up', response);