Can someone provide me an example of how to mute multiple hosts in New Relic using Python?

I want to mute few hosts on the basis of events using python

Hi, @vijay.k: What do you mean by “mute hosts”? New Relic muting rules apply to alert notifications, not hosts.

I meant the same @philweber I wanted to mute alert notifications triggered by few hosts. I need an example on how to use muting rules with python

You may find this post helpful:

1 Like

Hi @philweber Thanks for directing me to an example. I have made it working but i have one small issue. How can i pass variables from python like host, account id to nerdgraph i have tried few ways but its getting passed as null

import requests
import json

account_id = "XXXXXX"
host = ""

def nerdgraph_muteinstance():
    query = """
   mutation($host: String!) {
    accountId: XXXXX
    rule: {
      name: "host-1 mute rule"
      description: "Mute host-1 violations"
      enabled: true
      condition: {
        operator: AND
        conditions: [
            attribute: "tag.hostname"
            operator: EQUALS
            values: [$host]
  ) {
    variables = {
        host: host
    endpoint = ""
    headers = {
        'Content-Type': 'application/json',
        'Api-Key': 'api-key'
    response =, headers=headers, json={'query': query,'variable': json.dumps(variables)})
    if response.status_code == 200:
        dict_response = json.loads(response.content)
        raise Exception(
            f'Nerdgraph query failed with a {response.status_code}')


Here is my python script