Skip to main content

Create and consume app function

There are specific use cases when creating an app, such as querying third party data, heavy data processing, etc., where app functions have to be part of your app.

This guide shows you how to create an app function and how you can consume this function.

Create a new app function

The Dynatrace App Toolkit helps you to create a new app function easily. It provides you with a command that creates a function file with an example function.

Execute this command in your project directory:

npx dt-app generate function <function-name>

The Dynatrace App Toolkit creates a file with the function's name in the api directory within your root project directory. Every created TypeScript file in the api directory is exposed as an app function via the relative URL /api/filename.

The created function file already contains the function signature together with a very basic example:

api/hello-world.ts
export default async function (payload: unknown) {
return 'Hello world';
}

Consume the function

There are multiple places from which you can consume the app function. You can consume it in your Dynatrace App, from a Notebook, Workflow, or from outside the platform.

In your app code

To consume the app function in your app, you can either use the native JavaScript fetch function or use the call function provided by the @dynatrace-sdk/app-utils package:

src/App.tsx
const response = await fetch('/api/hello-world').then((res) => res.text());
console.log(response); // Hello world
src/App.tsx
import { functions } from '@dynatrace-sdk/app-utils';

const response = await functions.call('hello-world').then((res) => res.text());
console.log(response); // Hello world

From Notebooks or Workflows

With the @dynatrace-sdk/adhoc-utils package you can call app functions exposed by an app from Notebooks or Workflows.

The call function exposed by the functions namespace expects both the appId and the functionName as well as an optional data parameter to pass in the function payload:

import { functions } from '@dynatrace-sdk/adhoc-utils';

const response = await functions.call('my.app.id', 'hello-world').then((res) => res.text());

From outside the platform

App functions are exposed via a public API endpoint and can be called from anywhere outside the platform. When calling your function from outside the platform, you need to consider the following points:

  • You need to call the absolute API URL address
    • https://abc12345.apps.dynatrace.com/platform/app-engine/app-functions/v1/apps/<Your-App-ID>/api/<Your-Function-Name>
  • You need to take care of authentication by yourself. App function requests are authenticated via OAuth. Read Access platform APIs from outside for more details.

After obtaining a Bearer token from the single sign-on (SSO) token endpoint, a curl request to your app function could look like this:

curl --request GET 'https://abc12345.apps.dynatrace.com/platform/app-engine/app-functions/v1/apps/my.app.id/api/hello-world' \
--header 'Authorization: Bearer {your-bearer-token}'
Still have questions?
Find answers in the Dynatrace Community