Make API calls from functions

It is possible to make API calls to external sources from TypeScript v1, TypeScript v2 and Python functions, but doing so requires additional configuration. This configuration and external source usage are detailed below.

Configure access to external APIs

By default, functions are not allowed to call external APIs. To enable calling external systems from your function, you must configure a source in Data Connection to allow Foundry to connect with an external system.

For functions to connect to your source's external system securely, your source must be configured to enable exports and allow the import of your source into Code Repositories. Both of these can be configured by navigating to the source in Data Connection and opening the Connection settings section. Here you will find the Code import configuration tab, where you can also configure an API name if your source does not yet have one. This API name will be used to reference your source in code.

Use an external source in a function

To make API calls from a function, you must first import your source using the resource imports sidebar in a functions repository. You must then declare that your function uses the source.

Examples of this are shown below:

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 import { ExternalSystems } from "@foundry/functions-api"; import { MySource } from "@foundry/external-systems/sources"; export class MyExternalFunctions { @ExternalSystems({ sources: [MySource] }) @Function() public async myExternalFunction(): Promise<string> { const { url } = MySource.getHttpsConnection(); const response = await MySource.fetch(url); return response.text(); } }

You can test your function in live preview and use it to make external calls once published.

Third-party clients are not yet supported for serverless execution or live preview without overriding the fetch function or HTTP agent. To ensure your API calls function properly across all environments, you must use the relevant library methods to make requests with the correct configuration. Direct API calls to external sources or internal Foundry URLs are not guaranteed to work in all environments.

Access source attributes and credentials

You can access source attributes provided by each function type's corresponding library.

The example below shows how to obtain the base URL of the source in the example above.

Copied!
1 const { url } = MySource.getHttpsConnection();

You can also access additional secrets or credentials stored on the source by using the following syntax to access secrets:

Copied!
1 const secret = MySource.getSecret("MySecret");

Use the pre-configured clients

For sources that provide a REST API, the source object allows you to retrieve a client. This client will be pre-configured with the server and client certificates specified on the source, along with additional proxy configurations which allow egress from the environment functions are executed in. You should always use this client, if possible, to guarantee your function can egress to the source from all environments.

Copied!
1 const fetch = MySource.fetch;

Alternatively, you can use your own client or third-party libraries which make external requests, and use the source object to retrieve attributes and credentials.

TypeScript v2 functions provide a pre-configured HTTP agent as an additional integration point for usage with third party libraries which accept a custom HTTP agent.

The following example demonstrates retrieving this agent.

Copied!
1 2 3 import { getHttpAgent } from "@palantir/functions-sources"; const agent = await getHttpAgent(source);

Currently, it is not possible to access source attributes that are not credentials unless the source provides an HTTPS client. For example, you will not be able to access the hostname or other non-secret attributes on a PostgreSQL source.