This sample lists all network routes in a given project.
A route defines a path from VM instances in the VPC network to a specific destination. This destination can be inside or outside the VPC network.

## Code sample

### Go


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


For more information, see the
[Compute Engine Go API
reference documentation](https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).


To authenticate to Compute Engine, 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).

    import (
    	"context"
    	"fmt"
    	"io"

    	compute "cloud.google.com/go/compute/apiv1"
    	computepb "cloud.google.com/go/compute/apiv1/computepb"
    	"google.golang.org/api/iterator"
    )

    // listRoutes prints a list of routes created in given project.
    func listRoutes(w io.Writer, projectID string) error {
    	// projectID := "your_project_id"

    	ctx := context.Background()
    	client, err := compute.NewRoutesRESTClient(ctx)
    	if err != nil {
    		return fmt.Errorf("NewRoutesRESTClient: %w", err)
    	}
    	defer client.Close()

    	req := &computepb.ListRoutesRequest{
    		Project: projectID,
    	}
    	routes := client.List(ctx, req)

    	for {
    		instance, err := routes.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			return err
    		}
    		fmt.Fprintf(w, "- %s\n", *instance.Name)
    	}

    	return nil
    }

### Java


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


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


To authenticate to Compute Engine, 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).


    import com.google.cloud.compute.v1.ListRoutesRequest;
    import com.google.cloud.compute.v1.Route;
    import com.google.cloud.compute.v1.RoutesClient;
    import com.google.common.collect.Lists;
    import java.io.IOException;
    import java.util.List;

    public class ListRoute {

      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
        // Project ID or project number of the Cloud project you want to use.
        String projectId = "your-project-id";

        listRoutes(projectId);
      }

      // Lists routes from a project.
      public static List<Route> listRoutes(String projectId) throws IOException {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        try (RoutesClient routesClient = RoutesClient.create()) {
          ListRoutesRequest request = ListRoutesRequest.newBuilder()
                  .setProject(projectId)
                  .build();

          return Lists.newArrayList(routesClient.list(request).iterateAll());
        }
      }
    }

### Python


Before trying this sample, follow the Python setup instructions in the
[Compute Engine quickstart using
client libraries](https://docs.cloud.google.com/compute/docs/api/using-libraries).


For more information, see the
[Compute Engine Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/compute/latest).


To authenticate to Compute Engine, 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).

    from __future__ import annotations

    from collections.abc import Iterable

    from google.cloud import compute_v1


    def list_routes(
        project_id: str,
    ) -> Iterable[compute_v1.Route]:
        """
        Lists routes in project.

        Args:
            project_id: project ID or project number of the Cloud project you want to use.

        Returns:
            An iterable collection of routes found in given project.
        """

        route_client = compute_v1.RoutesClient()
        return route_client.list(project=project_id)

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