foundry-platform-sdk

The official Python library for the Foundry API


Keywords
Palantir, Foundry, SDK, Client, API, octo-correct-managed
License
Apache-2.0
Install
pip install foundry-platform-sdk==0.5.0

Documentation

Foundry Platform SDK

Supported Python Versions PyPI Version License

Warning

This SDK is incubating and subject to change.

The Foundry Platform SDK is a Python SDK built on top of the Foundry API. Review Foundry API documentation for more details.

Note

This Python package is automatically generated based off our OpenAPI specification.

Foundry Platform SDK vs. Ontology SDK

Palantir provides two different Python Software Development Kits (SDKs) for interacting with Foundry. Make sure to choose the correct SDK for your use case. As a general rule of thumb, any applications which leverage the Ontology should use the Ontology SDK for a superior development experience.

Important

Make sure to understand the difference between the Foundry SDK and the Ontology SDK. Review this section before continuing with the installation of this library.

Ontology SDK

The Ontology SDK allows you to access the full power of the Ontology directly from your development environment. You can generate the Ontology SDK using the Developer Console, a portal for creating and managing applications using Palantir APIs. Review the Ontology SDK documentation for more information.

Foundry Platform SDK

The Foundry Platform Software Development Kit (SDK) is generated from the Foundry API's OpenAPI specification file (see openapi.yml). The intention of this SDK is to encompass endpoints related to interacting with the platform itself. Although there are Ontology services included by this SDK, this SDK surfaces endpoints for interacting with Ontological resources such as object types, link types, and action types. In contrast, the OSDK allows you to interact with objects, links and Actions (for example, querying your objects, applying an action).

Installation

You can install the Python package using pip:

pip install foundry-platform-sdk

Then, import the package:

import foundry

Authorization and client initalization

There are two options for authorizing the SDK.

User token

Warning

User tokens are associated with your personal Foundry user account and must not be used in production applications or committed to shared or public code repositories. We recommend you store test API tokens as environment variables during development. For authorizing production applications, you should register an OAuth2 application (see OAuth2 Client below for more details).

You can pass in the hostname and token as keyword arguments when initializing the UserTokenAuth:

foundry_client = foundry.FoundryClient(
    auth=foundry.UserTokenAuth(
        hostname="example.palantirfoundry.com",
        token=os.environ["BEARER_TOKEN"],
    ),
    hostname="example.palantirfoundry.com",
)

OAuth2 Client

OAuth2 clients are the recommended way to connect to Foundry in production applications. Currently, this SDK natively supports the client credentials grant flow. The token obtained by this grant can be used to access resources on behalf of the created service user. To use this authentication method, you will first need to register a third-party application in Foundry by following the guide on third-party application registration.

To use the confidential client functionality, you first need to contstruct a ConfidentialClientAuth object and initiate the sign-in process using the sign_in_as_service_user method. As these service user tokens have a short lifespan, we automatically retry all operations one time if a 401 (Unauthorized) error is thrown after refreshing the token.

auth = foundry.ConfidentialClientAuth(
    client_id=os.environ["CLIENT_ID"],
    client_secret=os.environ["CLIENT_SECRET"],
    hostname="example.palantirfoundry.com",
    scopes=["api:read-data"],
)

auth.sign_in_as_service_user()

Important

Make sure to select the appropriate scopes when initializating the ConfidentialClientAuth. You can find the relevant scopes in the endpoint documentation.

After creating the ConfidentialClientAuth object, pass it in to the FoundryClient,

foundry_client = foundry.FoundryClient(auth=auth, hostname="example.palantirfoundry.com")

Quickstart

Follow the installation procedure and determine which authentication method is best suited for your instance before following this example. For simplicity, the UserTokenAuth class will be used for demonstration purposes.

from foundry import FoundryClient
from foundry import PalantirRPCException
from pprint import pprint

foundry_client = FoundryClient(
    auth=foundry.UserTokenAuth(...), hostname="example.palantirfoundry.com"
)

# DatasetRid | datasetRid
dataset_rid = "ri.foundry.main.dataset.c26f11c8-cdb3-4f44-9f5d-9816ea1c82da"

# Union[CreateBranchRequest, CreateBranchRequestDict] | Body of the request
create_branch_request = {"branchId": "my-branch"}


try:
    api_response = foundry_client.datasets.Dataset.Branch.create(
        dataset_rid,
        create_branch_request,
    )
    print("The create response:\n")
    pprint(api_response)
except PalantirRPCException as e:
    print("HTTP error when calling Branch.create: %s\n" % e)

Want to learn more about this Foundry SDK library? Review the following sections.

↳ Error handling: Learn more about HTTP & data validation error handling
↳ Pagination: Learn how to work with paginated endpoints in the SDK
↳ Static type analysis: Learn about the static type analysis capabilities of this library

Error handling

Data validation

The SDK employs Pydantic for runtime validation of arguments. In the example below, we are passing in a number to transactionRid which should actually be a string type:

foundry_client.datasets.Dataset.Branch.create(
    "ri.foundry.main.dataset.abc",
    create_branch_request={"branchId": "123", "transactionRid": 123},
)

If you did this, you would receive an error that looks something like:

pydantic_core._pydantic_core.ValidationError: 1 validation error for create
create_branch_request.transactionRid
  Input should be a valid string [type=string_type, input_value=123, input_type=int]
    For further information visit https://errors.pydantic.dev/2.5/v/string_type

To handle these errors, you can catch pydantic.ValidationError. To learn more, see the Pydantic error documentation.

Tip

Pydantic works with static type checkers such as pyright for an improved developer experience. See Static Type Analysis below for more information.

HTTP exceptions

When an HTTP error status is returned, a PalantirRPCException is thrown. All HTTP error exception classes inherit from ApiException.

from foundry import PalantirRPCException


try:
    api_response = foundry_client.datasets.Transaction.abort(dataset_rid, transaction_rid)
    ...
except PalantirRPCException as e:
    print("Another HTTP exception occurred: " + str(e))

This exception will have the following properties. See the Foundry API docs for details about the Foundry error information.

Property Type Description
name str The Palantir error name. See the Foundry API docs.
error_instance_id str The Palantir error instance ID. See the Foundry API docs.
parameters Dict[str, Any] The Palantir error parameters. See the Foundry API docs.

Pagination

When calling any iterator endpoints, we return a Pager class designed to simplify the process of working with paginated API endpoints. This class provides a convenient way to fetch, iterate over, and manage pages of data, while handling the underlying pagination logic.

To iterate over all items, you can simply create a Pager instance and use it in a for loop, like this:

for branch in foundry_client.datasets.Dataset.Branch.list(dataset_rid):
    print(branch)

This will automatically fetch and iterate through all the pages of data from the specified API endpoint. For more granular control, you can manually fetch each page using the associated page methods.

page = foundry_client.datasets.Dataset.Branch.page(dataset_rid)
while page.next_page_token:
    for branch in page.data:
        print(branch)

    page = foundry_client.datasets.Dataset.Branch.page(dataset_rid, page_token=page.next_page_token)

Static type analysis

This library uses Pydantic for creating and validating data models which you will see in the method definitions (see Documentation for Models below for a full list of models). All request parameters with nested objects use both TypedDicts and Pydantic models (either can be passed in) whereas responses only use Pydantic models. For example, here is how Branch.create method is defined in the datasets namespace:

    def create(
        self,
        dataset_rid: DatasetRid,
        create_branch_request: Union[CreateBranchRequest, CreateBranchRequestDict],
        *,
        request_timeout: Optional[Annotated[StrictInt, Field(gt=0)]] = None,
    ) -> Branch:
        ...

Tip

A Pydantic model can be converted into its TypedDict representation using the to_dict method. For example, if you handle a variable of type CreateBranchRequest and you called to_dict() on that variable you would receive a CreateBranchRequestDict variable.

If you are using a static type checker (for example, mypy, pyright), you get static type analysis for the arguments you provide to the function and with the response. For example, if you pass an int to branchId while calling create and then try to access branchId in returned Branch object (the property is actually called branch_id), you will get the following errors:

branch = foundry_client.datasets.Dataset.Branch.create(
    "ri.foundry.main.dataset.abc",
    create_branch_request={
        # ERROR: "Literal[123]" is incompatible with "BranchId"
        "branchId": 123
    },
)
# ERROR: Cannot access member "branchId" for type "Branch"
print(branch.branchId)

Common errors

This section will document any user-related errors with information on how you may be able to resolve them.

ApiFeaturePreviewUsageOnly

This error indicates you are trying to use an endpoint in public preview and have not set preview=True when calling the endpoint. Before doing so, note that this endpoint is in preview state and breaking changes may occur at any time.

During the first phase of an endpoint's lifecycle, it may be in Public Preview state. This indicates that the endpoint is in development and is not intended for production use.

Documentation for API endpoints

Namespace Resource Operation HTTP request
Datasets Branch create POST /v1/datasets/{datasetRid}/branches
Datasets Branch delete DELETE /v1/datasets/{datasetRid}/branches/{branchId}
Datasets Branch get GET /v1/datasets/{datasetRid}/branches/{branchId}
Datasets Branch list GET /v1/datasets/{datasetRid}/branches
Datasets Branch page GET /v1/datasets/{datasetRid}/branches
Datasets Dataset create POST /v1/datasets
Datasets Dataset delete_schema DELETE /v1/datasets/{datasetRid}/schema
Datasets Dataset get GET /v1/datasets/{datasetRid}
Datasets Dataset get_schema GET /v1/datasets/{datasetRid}/schema
Datasets Dataset read GET /v1/datasets/{datasetRid}/readTable
Datasets Dataset replace_schema PUT /v1/datasets/{datasetRid}/schema
Datasets File delete DELETE /v1/datasets/{datasetRid}/files/{filePath}
Datasets File get GET /v1/datasets/{datasetRid}/files/{filePath}
Datasets File list GET /v1/datasets/{datasetRid}/files
Datasets File page GET /v1/datasets/{datasetRid}/files
Datasets File read GET /v1/datasets/{datasetRid}/files/{filePath}/content
Datasets File upload POST /v1/datasets/{datasetRid}/files:upload
Datasets Transaction abort POST /v1/datasets/{datasetRid}/transactions/{transactionRid}/abort
Datasets Transaction commit POST /v1/datasets/{datasetRid}/transactions/{transactionRid}/commit
Datasets Transaction create POST /v1/datasets/{datasetRid}/transactions
Datasets Transaction get GET /v1/datasets/{datasetRid}/transactions/{transactionRid}
Ontologies ActionType get GET /v2/ontologies/{ontology}/actionTypes/{actionType}
Ontologies ActionType list GET /v2/ontologies/{ontology}/actionTypes
Ontologies ActionType page GET /v2/ontologies/{ontology}/actionTypes
Ontologies ObjectType get GET /v2/ontologies/{ontology}/objectTypes/{objectType}
Ontologies ObjectType get_outgoing_link_type GET /v2/ontologies/{ontology}/objectTypes/{objectType}/outgoingLinkTypes/{linkType}
Ontologies ObjectType list GET /v2/ontologies/{ontology}/objectTypes
Ontologies ObjectType list_outgoing_link_types GET /v2/ontologies/{ontology}/objectTypes/{objectType}/outgoingLinkTypes
Ontologies ObjectType page GET /v2/ontologies/{ontology}/objectTypes
Ontologies ObjectType page_outgoing_link_types GET /v2/ontologies/{ontology}/objectTypes/{objectType}/outgoingLinkTypes
Ontologies Ontology get GET /v2/ontologies/{ontology}
Ontologies Ontology get_full_metadata GET /v2/ontologies/{ontology}/fullMetadata
Ontologies Ontology list GET /v2/ontologies
Ontologies QueryType get GET /v2/ontologies/{ontology}/queryTypes/{queryApiName}
Ontologies QueryType list GET /v2/ontologies/{ontology}/queryTypes
Ontologies QueryType page GET /v2/ontologies/{ontology}/queryTypes
Security Group create POST /v2/security/groups
Security Group delete DELETE /v2/security/groups/{groupId}
Security Group get GET /v2/security/groups/{groupId}
Security Group list GET /v2/security/groups
Security Group page GET /v2/security/groups
Security Group search POST /v2/security/groups/search
Security GroupMember add POST /v2/security/groups/{groupId}/groupMembers/add
Security GroupMember list GET /v2/security/groups/{groupId}/groupMembers
Security GroupMember page GET /v2/security/groups/{groupId}/groupMembers
Security GroupMember remove POST /v2/security/groups/{groupId}/groupMembers/remove
Security GroupMembership list GET /v2/security/users/{userId}/groupMemberships
Security GroupMembership page GET /v2/security/users/{userId}/groupMemberships
Security User delete DELETE /v2/security/users/{userId}
Security User get GET /v2/security/users/{userId}
Security User get_current GET /v2/security/users/getCurrent
Security User list GET /v2/security/users
Security User page GET /v2/security/users
Security User profile_picture GET /v2/security/users/{userId}/profilePicture
Security User search POST /v2/security/users/search

Documentation for models

Contributions

This repository does not accept code contributions.

If you have any questions, concerns, or ideas for improvements, create an issue with Palantir Support.

License

This project is made available under the Apache 2.0 License.