The Remote API library allows any Python client to access services available to App Engine applications.
For example, if your App Engine application uses Google Cloud Datastore or Google Cloud Storage, a Python client could access those storage resources using the Remote API.
You can use the Remote API to access your application's data store from an app running on your local machine, or from a local interactive Remote API shell. The Remote API interacts with real services, so this access does use quota and billable resources.
The App Engine SDK for Python includes the Remote API library.
Enabling Remote API access in your app
The easiest way to enable the Remote API for your application is to use the
builtins directive in the app.yaml file for your app, which specifies the
default URL /_ah/remote_api/. However, you can instead use the url
directive in that same file to specify some other URL.
builtin
The builtins directive in the app.yaml file makes the Remote API
available at the default URL /_ah/remote_api:
URL
Using the url directive in app.yaml lets you specify a different URL for
use with the Remote API:
- url: /some-URL/*
script: google.appengine.ext.remote_api.handler.application
Make sure you deploy your application to App Engine after making this change.
Using the Remote API shell
The Python SDK includes a Remote API shell, which allows you to invoke Python commands on App Engine services used by your application. You don't need to supply any additional authentication, because this automatically uses the same credentials you used to upload the app to App Engine.
To start the Remote API shell:
-
Invoke the following command from a terminal window on your local machine:
[GAE-SDK-INSTALL-DIRECTORY]/remote_api_shell.py -s [YOUR-PROJECT-ID].appspot.comReplace
[GAE-SDK-INSTALL-DIRECTORY]with the path to the App Engine SDK for Python, and[YOUR-PROJECT-ID]with your project ID. -
In the interactive shell that is displayed, invoke the Python commands you want to run. For example, if your application uses Cloud Datastore, you could invoke the following ndb query to fetch 10 records:
>>> from google.appengine.ext import ndb >>> >>> # Fetch 10 keys from the datastore >>> ndb.Query().fetch(10, keys_only=True)
Using the Remote API in a local client
You can also use the Remote API in local applications to access services used by your app running in App Engine.
To use the Remote API in a local application:
-
Export the
PYTHONPATHenvironment variable for your Python directory, for example:export PYTHONPATH=/usr/somedir/v3/bin/python2.7Replace that path with the actual values for your python location.
-
Add your App Engine SDK for Python location to
PYTHONPATH:export GAE_SDK_ROOT="/usr/local/home/mydir/google_appengine" export PYTHONPATH=${GAE_SDK_ROOT}:${PYTHONPATH}Replace the SDK path shown above with your actual path to the App Engine SDK.
-
In your client code, import
dev_appserverand calldev_appserver.fix_sys_path()to ensure all of the App Engine SDK modules import correctly: -
Add the following
remote_api_stubcode to your application, making sure you pass it your project ID in your code:If you don't use the default URL
/_ah/remote_apifor the Remote API, you'll have to change the code above to reflect the URL you are using. For the definition and documentation forremote_api_stub.ConfigureRemoteApiForOAuth, see the SDK file[GAE-SDK-INSTALL-DIRECTORY]/google/appengine/ext/remote_api/remote_api_stub.py. -
Add any needed App Engine imports and Python code to access the desired App Engine services. The following sample code accesses the project's data store:
-
With your application deployed to App Engine, start your Remote API client:
python your-client.py YOUR-PROJECT-IDReplacing
your-client.pywith your client module, andYOUR-PROJECT-IDwith your project ID. This assumes your client accepts project ID as the command-line input, following theclient.pycode sample.