Skip to main content

Create and consume app function

For specific use cases within your app, such as querying third-party data, heavy data processing, and others, you need to utilize app functions as 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 use the useAppFunction hook provided by the @dynatrace-sdk/react-hooks package. Install it with the following command:

npm install @dynatrace-sdk/react-hooks

Now you can use it in your component:

src/App.tsx
import React from 'react';
import { useAppFunction } from '@dynatrace-sdk/react-hooks';

export const App = () => {
const response = useAppFunction({
name: 'hello-world',
responseType: 'text',
});

return (
<>
{response.isLoading && <p>Loading...</p>}
{response.data && <p>{response.data}</p>}
</>
);
};

From Notebooks or Workflows

The @dynatrace-sdk/adhoc-utils package allows you to 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