Rate this page:
This page introduces the Forge APIs.
Using the @forge/api
package, you'll learn how to make REST calls to an authenticated Confluence endpoint.
This is part 2 of 3 in this tutorial. Complete Part 1: Build a Confluence hello world app before working on this page.
In this section, you'll modify your app to call the Confluence REST API. Using the
Product Fetch API
from the @forge/api
package, you'll get the comments on a Confluence page in an array and print the number of comments to the console.
The @forge/api
package simplifies HTTP operations and contains other Forge APIs such as the
Storage and
Properties APIs.
For this tutorial, you’ll also use a
UI kit hook to get context
information about the page the app is on.
Modify your app’s code to call the Confluence REST API that gets the comments on a page. You’ll use the returned array to count the number of comments and write it to the Forge logs. You’ll use the Product Fetch API in the runtime to make the REST call. The runtime API simplifies some operations, including HTTP calls.
In the app’s top-level directory, install the @forge/api
package API by running:
1 2npm install @forge/api
Restart your tunnel to use the new npm modules by running:
1 2forge tunnel
Make sure your docker is running.
Navigate to the src
directory and open the index.jsx
file. Import the @forge/api
package by adding the following to the top of the file:
1 2import api, { route } from "@forge/api";
Copy the following code to create a function that calls the Confluence REST API by using
the @forge/api
package:
1 2const fetchCommentsForContent = async (contentId) => { const res = await api .asUser() .requestConfluence(route`/wiki/rest/api/content/${contentId}/child/comment`); const data = await res.json(); return data.results; };
This function takes the contentId
for a page to call the REST API with path
/rest/api/content/{contentId}/child/comment
.
Use the UI kit hook useProductContext
to get the contentId
to call fetchCommentsForContent
:
Add useProductContext
to the import statement as shown below:
1 2import ForgeUI, { render, Fragment, Text, useProductContext } from "@forge/ui";
Add the following code to the App
function directly before the return statement:
1 2const context = useProductContext();
Fetch the comments for the page and log the output in the App
function:
Add the UI kit hook useState
to the import statement.
Add the following code to the App
function directly above the return statement:
1 2const [comments] = useState(async () => await fetchCommentsForContent(context.contentId)); console.log(`Number of comments on this page: ${comments.length}`);
When you save the index.jsx
file, the tunnel output in the terminal
will display a permission-scope-required
error. To address this, you'll
need to add the required permissions first; this is covered in the
next section.
Your index.jsx
file should look like the following:
1 2import api, { route } from "@forge/api"; import ForgeUI, { render, Fragment, Text, Macro, useProductContext, useState } from '@forge/ui'; const fetchCommentsForContent = async (contentId) => { const res = await api .asUser() .requestConfluence(route`/wiki/rest/api/content/${contentId}/child/comment`); const data = await res.json(); return data.results; }; const App = () => { const context = useProductContext(); const [comments] = useState(async () => await fetchCommentsForContent(context.contentId)); console.log(`Number of comments on this page: ${comments.length}`); return ( <Fragment> <Text>Hello world!</Text> </Fragment> ); }; export const run = render( <Macro app={<App />} /> );
If you haven't enabled usage analytics yet, we recommend you do so using following command:
1 2forge settings set usage-analytics true
This command provides the consent required by Forge to collect data about your app's deployments and installations (including error data). This, in turn, helps us monitor the overall performance and reliability of Forge. The collected data also helps us make better decisions on improving Forge's feature set and performance.
For information about how Atlassian collects and handles your data, read our Privacy Policy.
Your app calls a remote resource; namely, the Confluence REST API. As such, you'll need to grant your app the right permissions. To do this, you'll need to add the required OAuth 2.0 scope to the app's manifest.
For more information on adding scopes, see Add scopes to call an Atlassian REST API.
In the steps below, you'll do this by using the forge lint
command. This command will automatically
add the required scope to your manifest.yml
file (in this case, read:confluence-content.summary
).
Run the following command:
1 2forge lint --fix
Whenever you change permissions, you must upgrade the app's installation. Stop your tunnel process and run these commands to deploy and install your change:
1 2forge deploy forge install --upgrade
Start the tunnel again:
1 2forge tunnel
Alternatively, you can also manually add required scopes to your manifest.yml
file.
Learn more about adding scopes to call an Atlassian REST API.
1 2invocation: 158b5987a4724ee5 index.run INFO 05:01:33.408 158b5987a4724ee5 Number of comments on this page: 1
The .asUser()
function inherits the product permissions of the user who has granted
access to the app. This can cause different API responses between different users in the same app.
In the next tutorial, you'll learn how to make changes to your app's frontend using the UI kit components of Forge.
Rate this page: