Table computations with the Earth Engine REST API

Note: The REST API contains new and advanced features that may not be suitable for all users. If you are new to Earth Engine, please get started with the JavaScript guide.

The Earth Engine REST API quickstart shows how to access blocks of pixels from an Earth Engine asset. The compute pixels example demonstrates how to apply a computation to the pixels before obtaining the result. This example demonstrates getting the mean of pixels in each image of an ImageCollection in each feature of a FeatureCollection. Specifically, this is a POST request to the computeFeatures endpoint.

Before you begin

Follow these instructions to:

  1. Apply for Earth Engine
  2. Create a Google Cloud project
  3. Enable the Earth Engine API on the project
  4. Create a service account
  5. Give the service account project level permission to perform Earth Engine computations

Note: To complete this tutorial, you will need a service account that is registered for Earth Engine access. See these instructions to register a service account before proceeding.

Authenticate to Google Cloud

The first thing to do is login so that you can make authenticated requests to Google Cloud. You will set the project at the same time. Follow the instructions in the output to complete the sign in.

PROJECT = 'your-project'

!gcloud auth login --project {PROJECT}

Obtain a private key file for your service account

You should already have a service account registered to use Earth Engine. If you don't, follow these instructions to get one. Copy the email address of your service account into the following cell. (The service account must already be registered to use Earth Engine). In the following cell, the gsutil command line is used to generate a key file for the service account. The key file will be created on the notebook VM.

KEY = 'key.json'

!gcloud iam service-accounts keys create {KEY} --iam-account {SERVICE_ACCOUNT}

Start an AuthorizedSession and test your credentials

Test the private key by using it to get credentials. Use the credentials to create an authorized session to make HTTP requests. Make a GET request through the session to check that the credentials work.

from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(KEY)
scoped_credentials = credentials.with_scopes(

session = AuthorizedSession(scoped_credentials)

url = ''

response = session.get(url)

from pprint import pprint
import json

Serialize a computation

Before you can send a request to compute something, the computation needs to be put into the Earth Engine expression graph format. The following demonstrates how to obtain the expression graph.

Authenticate to Earth Engine

Get Earth Engine scoped credentials from the service account. Use them to initialize Earth Engine.

import ee

# Get some new credentials since the other ones are cloud scope.
ee_creds = ee.ServiceAccountCredentials(SERVICE_ACCOUNT, KEY)

Define a computation

Prototype a simple computation with the client API. Note that the result of the computation is a FeatureCollection. To check that the computation can succeed without errors, get a value from the first Feature (the mean NDVI in the polygon).

# A collection of polygons.
states = ee.FeatureCollection('TIGER/2018/States')
maine = states.filter(ee.Filter.eq('NAME', 'Maine'))

# Imagery: NDVI vegetation index from MODIS.
band = 'NDVI'
images = ee.ImageCollection('MODIS/006/MOD13Q1').select(band)
image = images.first()

computation = image.reduceRegions(

# Print the value to test.

Serialize the expression graph

This will create an object that represents the Earth Engine expression graph (specifically, an Expression). In general, you should build these with one of the client APIs.

# Serialize the computation.
serialized = ee.serializer.encode(computation)

Send the request

Make a POST request to the computeFeatures endpoint. Note that the request contains the Expression, which is the serialized computation.

import json

url = '{}/table:computeFeatures'

response =
  url = url.format(PROJECT),
  data = json.dumps({'expression': serialized})

import json

The response contains the resultant FeatureCollection as GeoJSON, which can be consumed by other apps or processes.