Homeyscript to block Nextdns web categories (Parental control)

As a long time user of Nextdns on all our devices, I created this basic script to block/unblock web categories for the kids from Homey flows, using the api (NextDNS API Documentation | api). It may be useful for other Nextdns users as well so I thought I’d share it here. :slight_smile:

Save the script:

  • Set the Nextdns Api Key
  • Set a default Nextdns Profile Id

Use a “Run Script with argument” card:

  • Input: “Category|IsBlocked[|ProfileId]” (e.g. “Gambling|True|a1b2c3” or “Gambling|False”)
  • Output: NextDnsInfo tag with basic info

BTW: Make sure you add the category you want to control in Nextdns first!

// Block NextDns parental control Category using https://nextdns.github.io/api/
// Input: comma delimited list of "Category|IsBlocked[|ProfileId]" (e.g. "Gambling|True|a1b2c3, Gaming|False")
ApiKey = "xxxxxxxx"
DefaultProfileId = "abcdef" // Default nextdns Profile Id

// Get script arguments
myArgs = args[0] ?? "Gaming|False, Social-Networks|False, Video-Streaming|False, Gambling|True"
items = myArgs.split(',')

// Process all items
Info = ''
IsError = false
for (item of items) {
    item = item.split('|')
    Category = String(item[0]).trim().toLowerCase();
    IsBlocked = Boolean(item[1].trim().match(/^(true|yes|1)$/i))
    if (item[2]) { ProfileId = item[2].trim() } else { ProfileId = DefaultProfileId }

    // Set category using Nextdns API call
    body = { "active": IsBlocked }
    headers = { 'X-Api-Key': ApiKey, 'Content-Type': 'application/json' }
    uri = 'https://api.nextdns.io/profiles/' + ProfileId + '/parentalcontrol/categories/' + Category
    response = await fetch(uri, { method: 'PATCH', 'headers': headers, body: JSON.stringify(body) })
    //log(uri)
    //log(body)

    // Process result
    if (!response.ok) {
        IsError = true
        Info = Info + 'ERROR: ' + ((IsBlocked) ? 'Blocking ' : 'Unblocking ') + Category + ' (' + ProfileId + ')\n'
        log(`[ERROR ${response.status}] Make sure the category "${Category}" has been added to Parental Controls in Nextdns, and that ProfileId "${ProfileId}" is valid!`)
    } else {
        Info = Info + ((IsBlocked) ? 'Blocked ' : 'Unblocked ') + Category + ' (' + ProfileId + ')\n'
    }
}

// Output
log(Info)
await tag('NextDnsInfo', Info)
return !IsError

A similar script can be used for individual Apps/Services (Whatsapp, Tiktok, etc.) using this endpoint: uri = ‘https://api.nextdns.io/profiles/’ + ProfileId + ‘/parentalcontrol/services/’ + Service

BTW: A pity that the Logic HTTP Card does not support the PATCH command, otherwise we would not even need a script.