A newKeyinstance with the same data as the current one and an extra ID or name added.
from_legacy_urlsafe
from_legacy_urlsafe(urlsafe)
Convert urlsafe string to xref_Key.
This is intended to work with the "legacy" representation of a
datastore "Key" used within Google App Engine (a so-called
"Reference"). This assumes thaturlsafewas created within an App
Engine app via something likendb.Key(...).urlsafe().
Parameter
Name
Description
urlsafe
bytes or unicode
The base64 encoded (ASCII) string corresponding to a datastore "Key" / "Reference".
Convert to a base64 encode urlsafe string for App Engine.
This is intended to work with the "legacy" representation of a
datastore "Key" used within Google App Engine (a so-called
"Reference"). The returned string can be used as theurlsafeargument tondb.Key(urlsafe=...). The base64 encoded values
will have padding removed.
Parameter
Name
Description
location_prefix
str
The location prefix of an App Engine project ID. Often this value is 's', but may also be 'e', or other location prefixes currently unknown.
Returns
Type
Description
bytes
A bytestring containing the key encoded as URL-safe base64.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Class Key (2.21.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.21.0 (latest)](/python/docs/reference/datastore/latest/google.cloud.datastore.key.Key)\n- [2.20.2](/python/docs/reference/datastore/2.20.2/google.cloud.datastore.key.Key)\n- [2.19.0](/python/docs/reference/datastore/2.19.0/google.cloud.datastore.key.Key)\n- [2.18.0](/python/docs/reference/datastore/2.18.0/google.cloud.datastore.key.Key)\n- [2.17.0](/python/docs/reference/datastore/2.17.0/google.cloud.datastore.key.Key)\n- [2.16.1](/python/docs/reference/datastore/2.16.1/google.cloud.datastore.key.Key)\n- [2.15.2](/python/docs/reference/datastore/2.15.2/google.cloud.datastore.key.Key)\n- [2.14.0](/python/docs/reference/datastore/2.14.0/google.cloud.datastore.key.Key)\n- [2.13.2](/python/docs/reference/datastore/2.13.2/google.cloud.datastore.key.Key)\n- [2.12.0](/python/docs/reference/datastore/2.12.0/google.cloud.datastore.key.Key)\n- [2.11.1](/python/docs/reference/datastore/2.11.1/google.cloud.datastore.key.Key)\n- [2.10.0](/python/docs/reference/datastore/2.10.0/google.cloud.datastore.key.Key)\n- [2.9.0](/python/docs/reference/datastore/2.9.0/google.cloud.datastore.key.Key)\n- [2.8.3](/python/docs/reference/datastore/2.8.3/google.cloud.datastore.key.Key)\n- [2.7.2](/python/docs/reference/datastore/2.7.2/google.cloud.datastore.key.Key)\n- [2.6.2](/python/docs/reference/datastore/2.6.2/google.cloud.datastore.key.Key)\n- [2.5.1](/python/docs/reference/datastore/2.5.1/google.cloud.datastore.key.Key)\n- [2.4.0](/python/docs/reference/datastore/2.4.0/google.cloud.datastore.key.Key)\n- [2.3.0](/python/docs/reference/datastore/2.3.0/google.cloud.datastore.key.Key)\n- [2.2.0](/python/docs/reference/datastore/2.2.0/google.cloud.datastore.key.Key)\n- [2.1.6](/python/docs/reference/datastore/2.1.6/google.cloud.datastore.key.Key)\n- [2.0.1](/python/docs/reference/datastore/2.0.1/google.cloud.datastore.key.Key)\n- [1.15.5](/python/docs/reference/datastore/1.15.5/google.cloud.datastore.key.Key)\n- [1.14.0](/python/docs/reference/datastore/1.14.0/google.cloud.datastore.key.Key)\n- [1.13.2](/python/docs/reference/datastore/1.13.2/google.cloud.datastore.key.Key)\n- [1.12.0](/python/docs/reference/datastore/1.12.0/google.cloud.datastore.key.Key)\n- [1.11.0](/python/docs/reference/datastore/1.11.0/google.cloud.datastore.key.Key)\n- [1.10.0](/python/docs/reference/datastore/1.10.0/google.cloud.datastore.key.Key)\n- [1.9.0](/python/docs/reference/datastore/1.9.0/google.cloud.datastore.key.Key) \n\n Key(*path_args, **kwargs)\n\nAn immutable representation of a datastore Key.\n\n.. testsetup:: key-ctor\n\nfrom google.cloud import datastore\n\nproject = 'my-special-pony'\nclient = datastore.Client(project=project)\nKey = datastore.Key\n\nparent_key = client.key('Parent', 'foo')\n\nTo create a basic key directly:\n\n.. doctest:: key-ctor\n\u003e \u003e \u003e Key('EntityKind', 1234, project=project)\n\u003e \u003e \u003e \\\u003cKey('EntityKind', 1234), project=...\\\u003e\n\u003e \u003e \u003e Key('EntityKind', 'foo', project=project)\n\u003e \u003e \u003e \\\u003cKey('EntityKind', 'foo'), project=...\\\u003e\n\nThough typical usage comes via the\nxref_key factory:\n\n.. doctest:: key-ctor\n\u003e \u003e \u003e client.key('EntityKind', 1234)\n\u003e \u003e \u003e \\\u003cKey('EntityKind', 1234), project=...\\\u003e\n\u003e \u003e \u003e client.key('EntityKind', 'foo')\n\u003e \u003e \u003e \\\u003cKey('EntityKind', 'foo'), project=...\\\u003e\n\nTo create a key with a parent:\n\n.. doctest:: key-ctor\n\u003e \u003e \u003e client.key('Parent', 'foo', 'Child', 1234)\n\u003e \u003e \u003e \\\u003cKey('Parent', 'foo', 'Child', 1234), project=...\\\u003e\n\u003e \u003e \u003e client.key('Child', 1234, parent=parent_key)\n\u003e \u003e \u003e \\\u003cKey('Parent', 'foo', 'Child', 1234), project=...\\\u003e\n\nTo create a partial key:\n\n.. doctest:: key-ctor\n\u003e \u003e \u003e client.key('Parent', 'foo', 'Child')\n\u003e \u003e \u003e \\\u003cKey('Parent', 'foo', 'Child'), project=...\\\u003e\n\nTo create a key from a non-default database:\n\n.. doctest:: key-ctor\n\u003e \u003e \u003e Key('EntityKind', 1234, project=project, database='mydb')\n\u003e \u003e \u003e \\\u003cKey('EntityKind', 1234), project=my-special-pony, database=mydb\\\u003e\n\nProperties\n----------\n\n### database\n\nDatabase getter.\n\n### flat_path\n\nGetter for the key path as a tuple.\n\n### id\n\nID getter. Based on the last element of path.\n\n### id_or_name\n\nGetter. Based on the last element of path.\n\n### is_partial\n\nBoolean indicating if the key has an ID (or name).\n\n### kind\n\nKind getter. Based on the last element of path.\n\n### name\n\nName getter. Based on the last element of path.\n\n### namespace\n\nNamespace getter.\n\n### parent\n\nThe parent of the current key.\n\n### path\n\nPath getter.\n\nReturns a copy so that the key remains immutable.\n\n### project\n\nProject getter.\n\nMethods\n-------\n\n### __eq__\n\n __eq__(other)\n\nCompare two keys for equality.\n\nIncomplete keys never compare equal to any other key.\n\nCompleted keys compare equal if they have the same path, project,\ndatabase, and namespace.\n\n(Note that database=None is considered to refer to the default database.)\n\n### __hash__\n\n __hash__()\n\nHash this key for use in a dictionary lookup.\n\n### __ne__\n\n __ne__(other)\n\nCompare two keys for inequality.\n\nIncomplete keys never compare equal to any other key.\n\nCompleted keys compare equal if they have the same path, project,\ndatabase, and namespace.\n\n(Note that database=None is considered to refer to the default database.)\n\n### __repr__\n\n __repr__()\n\nString representation of this key.\n\nIncludes the project and database, but suppresses them if they are\nequal to the default values.\n\n### completed_key\n\n completed_key(id_or_name)\n\nCreates new key from existing partial key by adding final ID/name.\n\n### from_legacy_urlsafe\n\n from_legacy_urlsafe(urlsafe)\n\nConvert urlsafe string to xref_Key.\n\nThis is intended to work with the \"legacy\" representation of a\ndatastore \"Key\" used within Google App Engine (a so-called\n\"Reference\"). This assumes that `urlsafe` was created within an App\nEngine app via something like `ndb.Key(...).urlsafe()`.\n| **Note:** from_legacy_urlsafe only supports the default database.\n\n### to_legacy_urlsafe\n\n to_legacy_urlsafe(location_prefix=None)\n\nConvert to a base64 encode urlsafe string for App Engine.\n\nThis is intended to work with the \"legacy\" representation of a\ndatastore \"Key\" used within Google App Engine (a so-called\n\"Reference\"). The returned string can be used as the `urlsafe`\nargument to `ndb.Key(urlsafe=...)`. The base64 encoded values\nwill have padding removed.\n| **Note:** The string returned by `to_legacy_urlsafe` is equivalent, but not identical, to the string returned by `ndb`. The location prefix may need to be specified to obtain identical urlsafe keys.\n\n### to_protobuf\n\n to_protobuf()\n\nReturn a protobuf corresponding to the key."]]