App Identity PHP API Overview

The App Identity API lets an application discover its application ID (also called the project ID). Using the ID, an App Engine application can assert its identity to other App Engine Apps, Google APIs, and third-party applications and services. The application ID can also be used to generate a URL or email address, or to make a run-time decision.

Getting the application ID

The application ID can be found using the AppIdentityService::getApplicationId() method.

Getting the application hostname

By default, App Engine apps are served from URLs in the form http://<your_app_id>, where the app ID is part of the hostname. If an app is served from a custom domain, it may be necessary to retrieve the entire hostname component. You can do this using the AppIdentityService::getDefaultVersionHostname() method.

Asserting identity to other App Engine apps

If you want to determine the identity of the App Engine app that is making a request to your App Engine app, you can use the request header X-Appengine-Inbound-Appid. This header is added to the request by the URLFetch service and is not user modifiable, so it safely indicates the requesting application's ID, if present.

In order for this header to be added to the request, the app making the request must tell the URLFetch service to not follow redirects. App Engine will then automatically add the header to the HTTP response.

In your application handler, you can check the incoming ID by reading the X-Appengine-Inbound-Appid header and comparing it to a list of IDs allowed to make requests.

Asserting identity to Google APIs

Google APIs use the OAuth 2.0 protocol for authentication and authorization. The App Identity API can create OAuth tokens that can be used to assert that the source of a request is the application itself. The getAccessToken() method returns an access token for a scope, or list of scopes. This token can then be set in the HTTP headers of a call to identify the calling application.

The following example shows how to use the App Identity API to retrieve Google Calendar contacts using OAuth.
// Retrieves Google Calendar contacts using OAuth

use google\appengine\api\app_identity\AppIdentityService;

function setAuthHeader() {
  $access_token = AppIdentityService::getAccessToken('');
  return [sprintf('Authorization: OAuth %s', $access_token['access_token'])];

$get_contacts_url = '';
$headers = setAuthHeader();
$opts = [
  'http' => [
    'header' => implode("\r\n", $headers),

$context = stream_context_create($opts);

$response = file_get_contents($get_contacts_url, false, $context);

$xml = simplexml_load_string($response);

$email = $xml->author->email;
$service_account = AppIdentityService::getServiceAccountName();

if (strcmp($email, $service_account) != 0) {
  die(sprintf('%s does not match the service account name %s.',

Note that the application's identity is represented by the service account name, which is typically You can get the exact value by using the getServiceAccountName() method. For services which offer ACLs, you can grant the application access by granting this account access.

Asserting identity to third-party services

The token generated by getAccessToken() only works against Google services. However you can use the underlying signing technology to assert the identity of your application to other services. The signForApp() method will sign bytes using a private key unique to your application, and the getPublicCertificates() method will return certificates which can be used to validate the signature.

Getting the default Cloud Storage Bucket name

Each application can have one default Cloud Storage bucket, which includes 5GB of free storage and a free quota for I/O operations. The maximum storage of this bucket is 5GB, which you can increase by enabling billing for your app and paying for the extra storage making this a paid bucket.

To get the name of the default bucket, call CloudStorageTools::getDefaultGoogleStorageBucketName. Or, optionally, use the value #default# as the bucket name, where #default# will be automatically replaced at runtime by the application's default bucket name.

Was this page helpful? Let us know how we did:

Send feedback about...

App Engine standard environment for PHP