Adding installation parameters to a contentful App

559 views Asked by At

Struggling to find a working example or a document that explains how to set contentful app installation params. I can see how to get them from the SDK but settings them i cant.

any help is much appreciated.

3

There are 3 answers

0
user3669257 On BEST ANSWER

Most likely your app has a Config Location which means you are building UI that will be shown to the user during and after installation of your app. In this location, there is an SDK method called sdk.app.onConfigure. This method takes a function which will return an object which is called targetState.

targetState documentation can be found here.

Let's take a React Config app as an example where we will set {foo: 'bar'} as our installation parameters:

export default class Config extends Component<ConfigProps, ConfigState> {
  constructor(props: ConfigProps) {
    super(props);
    // letting the SDK know we have a configuration function which will
    // return `targetState`
    props.sdk.app.onConfigure(() => this.onConfigure());
  }

  // This method will be called when a user clicks on "Install"
  // or "Save" on the configuration screen.
  onConfigure = async () => {
    // the object returned here is what Contentful calls `targetState`.
    // it is simply a configuration object set when the app is installed or updated
    return {
        parameters: { installation: { foo: 'bar' } }
    };
  };

In the example above, when a user hits "Install" or "Save" on the app's Config location, the installation parameter object of {foo: 'bar'} will be saved and can then be accessed in other app locations via the SDK.

On the off chance you are purely using the API to create or modify an AppInstallation, you can use the Content Management API to update the app's parameters as described in the documentation here.

0
Elleken On

Late answer but i also struggled with it. I found a tutorial here: https://dev.to/wiommi/how-i-built-a-contentful-app-combined-with-commerce-js-iii-33fo

They are added manually in ConfigScreen.tsx

You need to add them to your interface

export interface AppInstallationParameters {}

fx:

export interface AppInstallationParameters {
  apiKey?: string;
  projectId?: string;
}

And then set them manually with fx.

setParameters({ ...parameters, [PARAMETERNAME]: [PARAMETERVALUE] });
0
Abdul Azeem On

You can also setup installation parameters at installation time, however, you need to install using this endpoint and provide the parameters:

PUT /spaces/{space_id}/environments/{environment_id}/app_installations/{app_definition_id}
{
  "parameters": {
    "hello": "world"
  }
}

See https://www.contentful.com/developers/docs/references/content-management-api/#/reference/app-installations/app-installation/install-or-update-an-app/console/curl