Learn how to create a user interface for your app and fetch data from it.
When developing a VTEX IO app, you can create an interface for accepting custom user settings. The app settings interface allows you to retrieve data provided by the user and enable or modify an app behavior based on it. The configuration possibilities depend on the type of data collected by the interface and the intended use.
This guide outlines the process of creating such an interface and using the data input by users within your app.
Usage example
The app settings interface displays configuration forms for installed apps in the App Store. This interface is defined via the settingsSchema
field on manifest.json
, where an app may declare the schema of its configuration.
See the Google Tag Manager app interface in the example below, with the appropriate configuration of the settingsSchema
field in the manifest.json
file.
After building the settings interface for your app, you need to consume the data provided by the app’s user. Consider the Google Tag Manager example above retrieving the GTM ID.
Based on the given example, below is a step-by-step guide for creating an interface for your app and retrieving the data to use within the app.
Instructions
Step 1 - Creating the settings interface
Open your app project in any code editor of your choice and open the manifest.json
file.
Define the settingsSchema
object, declaring a JSON Schema containing the fields that meet your business needs. See the JSON Schema documentation to build the settingsSchema
field correctly.
The JSON Schema offers a predefined model that outlines how the settings should be formatted in your app. Note that, according to the JSON Schema specification, the types accepted within a property include:
string
,number
,integer
,boolean
,object
,array
, ornull
. It is also possible to use arrays of types (e.g.,[string, number]
) to indicate that the property accepts multiple types.
Considering the example of the Google Tag Manager interface previously presented, the settingsSchema
object was configured as follows:
_22 "settingsSchema": {_22 "title": "Google Tag Manager",_22 "type": "object",_22 "bindingBounded": true,_22 "properties": {_22 "gtmId": {_22 "title": "Google Tag Manager",_22 "description": "Enter the ID (GTM-XXXX) from your Google Tag Manager",_22 "type": "string"_22 },_22 "allowCustomHtmlTags": {_22 "title": "Allow Custom HTML tags",_22 "description": "Beware that using Custom HTML tags can drastically impact the store's performance",_22 "type": "boolean"_22 },_22 "sendGA4Events": {_22 "title": "Send Google Analytics 4 Events",_22 "description": "When this setting is active, also send the GA4 version of the events",_22 "type": "boolean"_22 }_22 }_22 },
This code defines a schema for the settings UI that interacts with the Google Tag Manager app. The configured fields are described below:
title
: App name shown in the app settings interface. It is recommended to use the same app name configured in thetitle
field of the manifest.json file.type
: JSON Schema type (this must always be set asobject
).description
(optional): Description of thesettingsSchema
field.properties
: Key-value pairs that define the fields that will be displayed to the app users, with their own definition within this object. Each defined key accepts the followingproperties
:title
,description
, andtype
.gtmId
: String-type property that represents the Google Tag Manager ID. In the settings UI, this property is shown as a text box.allowCustomHtmlTags
: Boolean-type property that determines whether custom HTML tags are allowed. In the settings UI, this property is shown as a checkbox.sendGA4Events
: Boolean-type property that indicates whether to send Google Analytics 4 events. In the settings UI, this property is shown as a checkbox.
Step 2 - Retrieving and consuming the app settings data
On your app’s code, you can retrieve the app settings in two ways:
- Frontend apps: Fetch the data using a GraphQL query.
- Backend apps: Use the
getAppSettings
method from thectx.clients.apps
client.
Below is an example of each case.
Frontend apps
- In the
manifest.json
file, add thevtex.apps-graphql
3.x within thedependencies
field. - In the
react
directory, create a new folder namedqueries
. - Create a new
graphql
file (e.g.,AppSettings.graphql
) and query the settings options as in the following example.
_10query getSettings {_10 publicSettingsForApp(app: "{vendor}.{app.name}", version: "0.x")_10 @context(provider: "vtex.apps-graphql") {_10 message_10 }_10}
Replace
vendor
,app.name
, and0.x
with values based on your scenario.
The example above is a GraphQL query that fetches the data of a specific app declared in the arguments of publicSettingsForApp
field: the app and its version. The configured fields are described below:
@context(provider: “vtex.apps-graphql”)
: The context for the query. In this case, it must be configured as “vtex.apps-graphql”.message
: The message field to be included in the query result, which is a field within thepublicSettingsForApp
object. Note that the message may vary depending on the app's settingsSchema.
This query will return an object like the one below:
_10{_10 "data": {_10 "publicSettingsForApp": {_10 "message": "{\"maxNumberOfItemsToCompare\":2}"_10 }_10 }_10}
Please be aware that only settings labeled with
public
access
in thesettingsSchema
will be returned.
- Open the
tsx
file of the React component that will consume this information (e.g.,ProductComparisonContext.tsx
) and import the GraphQL query:
_10import AppSettings from './queries/AppSettings.graphql'
- Parse and use the defined query (e.g.,
AppSettings
) value based on your needs. Check the example below:
_24const { data: appSettingsData } = useQuery(AppSettings, {_24 variables: {_24 // eslint-disable-next-line no-undef_24 version: process.env.VTEX_APP_VERSION,_24 },_24 ssr: false,_24 })_24_24 useEffect(() => {_24 const appSettings = JSON.parse(_24 pathOr(`\{\}`, ['publicSettingsForApp', 'message'], appSettingsData)_24 )_24_24 dispatch({_24 type: 'SET_MAX_LIMIT',_24 args: {_24 maxLimit: pathOr(_24 MAX_ITEMS_TO_COMPARE,_24 ['maxNumberOfItemsToCompare'],_24 appSettings_24 ),_24 },_24 })_24 }, [appSettingsData])
The code above interacts with an API through a GraphQL query, which is managed by the useQuery
hook, to fetch the app settings. It then parses and updates the app’s state with these settings using a useEffect
hook whenever the settings data changes.
Backend apps
- Open your
node
app in any code editor of your choice. - Declare a new function (e.g.,
getConfig
) and use thegetAppSettings
method to retrieve and return the app settings data. Consider the example below:
_15export async function getConfig: async (_: any, __: any, ctx: Context) => {_15 const {_15 clients: { apps },_15 vtex: { logger },_15 } = ctx_15_15 const appId = process.env.VTEX_APP_ID_15_15 return apps.getAppSettings(appId).catch((error) => {_15 logger.error({_15 message: 'getConfig-getAppSettings-error',_15 error,_15 })_15 })_15 },
The getConfig
function returns a promise as it fetches the app settings and handles errors. It retrieves an app ID from environment variables, uses it to request settings, and logs any errors that occur.
- Import the defined function and consume it based on your needs.