Demonstrates how to list alerting policies.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Manage alerting policies by API](https://docs.cloud.google.com/monitoring/alerts/using-alerting-api)

## Code sample

### C#


To authenticate to Monitoring, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    static void ListAlertPolicies(string projectId)
    {
        var client = AlertPolicyServiceClient.Create();
        var response = client.ListAlertPolicies(new ProjectName(projectId));
        foreach (AlertPolicy policy in response)
        {
            Console.WriteLine(policy.Name);
            if (policy.DisplayName != null)
            {
                Console.WriteLine(policy.DisplayName);
            }
            if (policy.Documentation?.Content != null)
            {
                Console.WriteLine(policy.Documentation.Content);
            }
            Console.WriteLine();
        }
    }

### Go


To authenticate to Monitoring, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).


    // listAlertPolicies lists the alert policies in the project.
    func listAlertPolicies(w io.Writer, projectID string) error {
    	ctx := context.Background()
    	client, err := monitoring.NewAlertPolicyClient(ctx)
    	if err != nil {
    		return err
    	}
    	defer client.Close()

    	req := &monitoringpb.ListAlertPoliciesRequest{
    		Name: "projects/" + projectID,
    		// Filter:  "", // See https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
    		// OrderBy: "", // See https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
    	}
    	it := client.ListAlertPolicies(ctx, req)
    	for {
    		resp, err := it.Next()
    		if err == iterator.Done {
    			fmt.Fprintln(w, "Done")
    			break
    		}
    		if err != nil {
    			return err
    		}
    		fmt.Fprintf(w, "  Name: %q\n", resp.GetName())
    		fmt.Fprintf(w, "  Display Name: %q\n", resp.GetDisplayName())
    		fmt.Fprintf(w, "  Documentation Content: %q\n\n", resp.GetDocumentation().GetContent())
    	}
    	return nil
    }

### Java


To authenticate to Monitoring, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    private static void listAlertPolicies(String projectId) throws IOException {
      try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) {
        ListAlertPoliciesPagedResponse response = client.listAlertPolicies(ProjectName.of(projectId));

        System.out.println("Alert Policies:");
        for (AlertPolicy policy : response.iterateAll()) {
          System.out.println(
              String.format("\nPolicy %s\nalert-id: %s", policy.getDisplayName(), policy.getName()));
          int channels = policy.getNotificationChannelsCount();
          if (channels > 0) {
            System.out.println("notification-channels:");
            for (int i = 0; i < channels; i++) {
              System.out.println("\t" + policy.getNotificationChannels(i));
            }
          }
          if (policy.hasDocumentation() && policy.getDocumentation().getContent() != null) {
            System.out.println(policy.getDocumentation().getContent());
          }
        }
      }
    }

### Node.js


To authenticate to Monitoring, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Imports the Google Cloud client library
    const monitoring = require('https://docs.cloud.google.com/nodejs/docs/reference/monitoring/latest/overview.html');

    // Creates a client
    const client = new monitoring.https://docs.cloud.google.com/nodejs/docs/reference/monitoring/latest/overview.html();

    async function listPolicies() {
      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // const projectId = 'YOUR_PROJECT_ID';

      const listAlertPoliciesRequest = {
        name: client.projectPath(projectId),
      };
      const [policies] = await client.listAlertPolicies(listAlertPoliciesRequest);
      console.log('Policies:');
      policies.forEach(policy => {
        console.log(`  Display name: ${policy.displayName}`);
        if (policy.documentation && policy.documentation.content) {
          console.log(`     Documentation: ${policy.documentation.content}`);
        }
      });
    }
    listPolicies();

### PHP


To authenticate to Monitoring, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    use Google\Cloud\Monitoring\V3\Client\AlertPolicyServiceClient;
    use Google\Cloud\Monitoring\V3\ListAlertPoliciesRequest;

    /**
     * Adds a new column to the Albums table in the example database.
     * Example:
     * ```
     * alert_list_policies($projectId);
     * ```
     *
     * @param string $projectId Your project ID
     */
    function alert_list_policies($projectId)
    {
        $projectName = 'projects/' . $projectId;
        $alertClient = new AlertPolicyServiceClient([
            'projectId' => $projectId,
        ]);
        $listAlertPoliciesRequest = (new ListAlertPoliciesRequest())
            ->setName($projectName);

        $policies = $alertClient->listAlertPolicies($listAlertPoliciesRequest);
        foreach ($policies->iterateAllElements() as $policy) {
            printf('Name: %s (%s)' . PHP_EOL, $policy->getDisplayName(), $policy->getName());
        }
    }

### Python


To authenticate to Monitoring, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    def list_alert_policies(project_name):
        """List alert policies in a project.

        Arguments:
            project_name (str): The Google Cloud Project to use. The project name
                must be in the format - 'projects/<PROJECT_NAME>'.
        """

        client = monitoring_v3.AlertPolicyServiceClient()
        policies = client.list_alert_policies(name=project_name)
        print(
            str(
                tabulate.tabulate(
                    [(policy.name, policy.display_name) for policy in policies],
                    ("name", "display_name"),
                )
            )
        )

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=cloudmonitoring).