List all available event providers in a specified project and location.

## Code sample

### Go


To authenticate to Eventarc, 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.md.txt).

```go

import (
	"context"
	"fmt"
	"io"

	eventarc "cloud.google.com/go/eventarc/apiv1"
	eventarcpb "cloud.google.com/go/eventarc/apiv1/eventarcpb"
	"google.golang.org/api/iterator"
)

// listProviders retrieves a list of Eventarc providers in a given project and location.
func listProviders(ctx context.Context, w io.Writer, projectID string, locationID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)

	client, err := eventarc.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient failed: %w", err)
	}
	defer client.Close()

	req := &eventarcpb.ListProvidersRequest{
		Parent: parent,
	}

	it := client.ListProviders(ctx, req)
	for {
		provider, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("failed to iterate providers: %w", err)
		}

		fmt.Fprintf(w, "Provider: %s (Name: %s)\n", provider.GetDisplayName(), provider.GetName())
	}

	return nil
}
```

### Java


To authenticate to Eventarc, 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.md.txt).

```java
import com.google.cloud.eventarc.v1.EventarcClient;
import com.google.cloud.eventarc.v1.Provider;

public class ListProviders {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    listProviders(projectId, locationId);
  }

  public static void listProviders(String projectId, String locationId) throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, locationId);
      for (Provider provider : client.listProviders(parent).iterateAll()) {
        System.out.println("Provider: " + provider.getName());
      }
    }
  }
}
```

### Node.js


To authenticate to Eventarc, 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.md.txt).

```javascript
'use strict';

const {EventarcClient} = require('@google-cloud/eventarc');
const {status} = require('@grpc/grpc-js');

/**
 * Lists Eventarc providers in a specified location.
 */
async function listProviders() {
  const client = new EventarcClient();

  try {
    const projectId = await client.getProjectId();
    const location = 'us-central1';
    const parent = `projects/${projectId}/locations/${location}`;

    const request = {
      parent,
    };

    const [providers] = await client.listProviders(request);

    console.log(`Found ${providers.length} provider(s):`);
    for (const provider of providers) {
      console.log(`- ${provider.name} (${provider.displayName})`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument: ${err.message}`);
    } else {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { listProviders };
```

### Python


To authenticate to Eventarc, 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.md.txt).

```python
import google.api_core.exceptions
from google.cloud import eventarc_v1

client = eventarc_v1.EventarcClient()


def list_providers(project_id: str, location_id: str) -> None:
    """List Eventarc providers in a specified location.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud location or region (for example, 'global' or 'us-central1').
    """
    parent = f"projects/{project_id}/locations/{location_id}"

    request = eventarc_v1.ListProvidersRequest(
        parent=parent,
    )

    try:
        response = client.list_providers(request=request)
        for provider in response:
            print(f"Provider name: {provider.name}")
            print(f"Display name: {provider.display_name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to list providers: {error.message}")
```

## 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.md.txt?product=eventarc).