Skip to content

Application Managed Sessions - Planet API Key

Planet API Key Sessions

Legacy applications that need to continue to support Planet API keys may do so until API keys are deprecated. This method should not be adopted for new development if possible.

Examples - Planet API Keys

In Memory Session State

Once provided with an API key, an application may operate with the API key in memory indefinitely without the need to prompt the user for re-authentication.

Access APIs using Planet API keys in memory
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import json
import planet


def example_main():
    # Create an auth context with the specified API key
    plsdk_auth = planet.Auth.from_key(
        key="__PLANET_API_KEY_MUST_BE_END_USER_SUPPLIED__")

    # Explicit login is not required for API key use. The above sufficient.
    # plsdk_auth.user_login()

    # Create a Planet SDK object that uses the loaded auth session
    sess = planet.Session(plsdk_auth)
    pl = planet.Planet(sess)

    # Use the SDK to call Planet APIs
    for item in pl.data.list_searches():
        print(json.dumps(item, indent=2, sort_keys=True))


if __name__ == '__main__':
    example_main()

Version 2 Compatibility

The SDK continues to support files written by version 2 of the SDK to save auth state.

Access APIs using Planet API keys using the on disk file format used by older versions of the SDK
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import json
import planet


def example_main():
    # Create an auth context with a Planet API key loaded from the
    # specified file that was created with older versions of the SDK
    plsdk_auth = planet.Auth.from_file("legacy_api_key_file.json")

    # Explicit login is not required for API key use. The above is sufficient.
    # plsdk_auth.user_login()

    # Create a Planet SDK object that uses the loaded auth session
    sess = planet.Session(plsdk_auth)
    pl = planet.Planet(sess)

    # Use the SDK to call Planet APIs.
    for item in pl.data.list_searches():
        print(json.dumps(item, indent=2, sort_keys=True))


if __name__ == '__main__':
    example_main()

Legacy API Key file example
1
2
3
{
    "key": "__PLANET_API_KEY_MUST_BE_END_USER_SUPPLIED__"
}

Session State Shared with CLI

Access APIs using Planet API keys with CLI managed shared state on disk
1
2
# No example of this use case provided at this time.
# The use of M2M OAuth sessions is encouraged over the use of API keys.

Session State Saved to Application Storage

Access APIs using Planet API keys with sessions persisted to application provided storage
1
2
# No example of this use case provided at this time.
# The use of M2M OAuth sessions is encouraged over the use of API keys.

Back to top