List the schema resources in a project.

## Explore further


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

- [List schemas for a topic](https://docs.cloud.google.com/pubsub/docs/list-schemas.md.txt)

## Code sample

### C++


Before trying this sample, follow the C++ setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub C++ API
reference documentation](https://googleapis.dev/cpp/google-cloud-pubsub/latest/).


To authenticate to Pub/Sub, 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).

```c++
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::SchemaServiceClient client, std::string const& project_id) {
  auto const parent = google::cloud::Project(project_id).FullName();
  for (auto& s : client.ListSchemas(parent)) {
    if (!s) throw std::move(s).status();
    std::cout << "Schema: " << s->DebugString() << "\n";
  }
}
```

### C#


Before trying this sample, follow the C# setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.PubSub.V1/latest).


To authenticate to Pub/Sub, 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).

```c#

using Google.Api.Gax.ResourceNames;
using Google.Cloud.PubSub.V1;
using System.Collections.Generic;

public class ListSchemasSample
{
    public IEnumerable<Schema> ListSchemas(string projectId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        ProjectName projectName = ProjectName.FromProject(projectId);
        var schemas = schemaService.ListSchemas(projectName);
        return schemas;
    }
}
```

### Go


Before trying this sample, follow the Go setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub Go API
reference documentation](https://godoc.org/cloud.google.com/go/pubsub).


To authenticate to Pub/Sub, 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"

	pubsub "cloud.google.com/go/pubsub/v2/apiv1"
	"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb"
	"google.golang.org/api/iterator"
)

func listSchemas(w io.Writer, projectID string) ([]*pubsubpb.Schema, error) {
	// projectID := "my-project-id"
	ctx := context.Background()
	client, err := pubsub.NewSchemaClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("pubsub.NewSchemaClient: %w", err)
	}
	defer client.Close()

	var schemas []*pubsubpb.Schema

	req := &pubsubpb.ListSchemasRequest{
		Parent: fmt.Sprintf("projects/%s", projectID),
		View:   pubsubpb.SchemaView_FULL,
	}
	schemaIter := client.ListSchemas(ctx, req)
	for {
		sc, err := schemaIter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("schemaIter.Next: %w", err)
		}
		fmt.Fprintf(w, "Got schema: %#v\n", sc)
		schemas = append(schemas, sc)
	}

	fmt.Fprintf(w, "Got %d schemas", len(schemas))
	return schemas, nil
}
```

### Java


Before trying this sample, follow the Java setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-pubsub/latest/overview).


To authenticate to Pub/Sub, 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.pubsub.v1.SchemaServiceClient;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Schema;
import java.io.IOException;

public class ListSchemasExample {
  public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";

    listSchemasExample(projectId);
  }

  public static void listSchemasExample(String projectId) throws IOException {
    ProjectName projectName = ProjectName.of(projectId);

    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
      for (Schema schema : schemaServiceClient.listSchemas(projectName).iterateAll()) {
        System.out.println(schema);
      }
      System.out.println("Listed schemas.");
    }
  }
}
```

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub Node.js API
reference documentation](https://googleapis.dev/nodejs/pubsub/latest).


To authenticate to Pub/Sub, 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).


    // Imports the Google Cloud client library
    const { PubSub } = require("@google-cloud/pubsub");

    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();

    async function listSchemas() {
      for await (const s of pubSubClient.listSchemas()) {
        console.log(s.name);
      }
      console.log('Listed schemas.');
    }

    // Imports the Google Cloud client library
    const { PubSub } = require("@google-cloud/pubsub");

    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();

    async function listSchemas() {
      for await (const s of pubSubClient.listSchemas()) {
        console.log(s.name);
      }
      console.log('Listed schemas.');
    }

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub Node.js API
reference documentation](https://googleapis.dev/nodejs/pubsub/latest).


To authenticate to Pub/Sub, 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).


    // Imports the Google Cloud client library
    import {PubSub} from '@google-cloud/pubsub';

    // Creates a client; cache this for further use
    const pubSubClient = new PubSub();

    async function listSchemas() {
      for await (const s of pubSubClient.listSchemas()) {
        console.log(s.name);
      }
      console.log('Listed schemas.');
    }

### PHP


Before trying this sample, follow the PHP setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/latest/pubsub/pubsubclient).


To authenticate to Pub/Sub, 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).

```php
use Google\Cloud\PubSub\PubSubClient;

/**
 * List schemas in the project.
 *
 * @param string $projectId
 */
function list_schemas($projectId)
{
    $pubsub = new PubSubClient([
        'projectId' => $projectId,
    ]);

    $schemas = $pubsub->schemas();
    foreach ($schemas as $schema) {
        printf('Schema name: %s' . PHP_EOL, $schema->name());
    }
}
```

### Ruby


Before trying this sample, follow the Ruby setup instructions in the
[Pub/Sub quickstart using
client libraries](https://docs.cloud.google.com/pubsub/docs/quickstart-client-libraries).


For more information, see the
[Pub/Sub Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html).


To authenticate to Pub/Sub, 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).

```ruby

pubsub = Google::Cloud::PubSub.new
schema_service = pubsub.schemas

schemas = schema_service.list_schemas \
  parent: pubsub.project_path,
  view: Google::Cloud::PubSub::V1::SchemaView::FULL

puts "Schemas in project:"
schemas.each do |schema|
  puts schema.name
end
```

### Rust

```rust
use google_cloud_gax::paginator::ItemPaginator;
use google_cloud_pubsub::client::SchemaService;
use google_cloud_pubsub::model::SchemaView;

pub async fn sample(client: &SchemaService, project: &str) -> anyhow::Result<()> {
    let mut schemas = client
        .list_schemas()
        .set_parent(format!("projects/{}", project))
        .set_view(SchemaView::Full)
        .by_item();

    println!("listing schemas");
    while let Some(schemas) = schemas.next().await.transpose()? {
        println!("{schemas:?}");
    }
    println!("DONE");
    Ok(())
}
```

## 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=pubsub).