Question on calling Rest APIs in my plagin in Draft mode while the api works well using temporary access token in Dev mode.

39 views
Skip to first unread message

Chunjen Chen

unread,
Jan 5, 2024, 4:24:25 PM1/5/24
to Zengine Development

I developed a plugin in which it calls Rest APIs well by adding access_token in the ajax url. ( ex. 'https://api.zenginehq.com/v1/forms/' + value.id + '/records?limit=100' + access_token; )

 

I got the access_token in the https://platform.zenginehq.com/account/developer page but it will expired within 1 hour.

 

I need to know how to set my api url in which I don't need to update my access token every hour by myself, since this plugin is going to publish to be used for few people.

 

I removed the access_token, after i read  <https://groups.google.com/g/zengine-development/c/O7EB-drRiX8>  posted by Wes on Oct 18, 2023, 2:09:45 PM.  It mentioned 'while approach two will run on our servers and will automatically get an access token.' Unfortunately , I got the result  '400 Bad Request'. So I think I still need to have access token.

 

May I know how to call rest API in a plugin in which I don't have to update the access token every hour and allow other people use it?

 

Thank you very much.

Allen

Wes

unread,
Jan 8, 2024, 9:54:10 AM1/8/24
to Zengine Development
If you use plugins, you would use the plugin libraries for znData or znHttp to make API requests. If you are using a frontend plugin, it will use the token of the acting user when making requests. If you are using a backend plugin that is offline enabled, the znHttp library will be provided with a short lived access token. 


If you create an offline enabled backend plugin, make sure to add some type of secret verification so that only your approved integration can access it. 

Chunjen Chen

unread,
Jan 8, 2024, 2:39:52 PM1/8/24
to Zengine Development
Thanks for your rapid reply, Wes.
my code below will get into the catch statement to show 'Backend Service Fail'. it is just trying to get count of records for a form. I assume i don't need to put any options.
1. Do you have any advice for me?
2. How to debug the  error? will it return any object which I can use catch((error) => { ...});


               let myUrl = 'https://api.zenginehq.com/v1/forms/' + value.id + '/records/count';

                znPluginData({

                    namespace: context.plugin.namespace,

                    method: 'get',

                    route: myUrl

                    //options: {

                    //    params: {

                    //        //id: context.workspace.forms[0].id

                    //    }

                    //}

                }).then((result) => {

                    if (result.totalCount > 0) {

                        let totalRecordsInThisForm = result.totalCount == undefined ? 0 : result.totalCount;

                        /*let paginate = 1;*/

                        let maxRowsZenginApiLoaded = 100;

                        let totalPages = (totalRecordsInThisForm % maxRowsZenginApiLoaded == 0) ? Math.floor(totalRecordsInThisForm / maxRowsZenginApiLoaded) : Math.floor(totalRecordsInThisForm / maxRowsZenginApiLoaded) + 1;

                    }

                 }).catch(() => {

                    znMessage('Backend Service Fail', 'error');

                });

The attached file is the screenshot where the error happened. Not sure if it is helpful.
Thank you very much.
Allen
dump.PNG

Wes

unread,
Jan 9, 2024, 11:59:26 AM1/9/24
to Zengine Development
The libraries are not wrappers for a request library. You pass our resources and params instead of urls.

Try this format:

znData('FormRecords').query({ formId: 123, limit: 100, page: 1 }).then((records) => console.log(records))

Also note that znPluginData library is only for making requests to a backend plugin service associated with your plugin.

Chunjen Chen

unread,
Jan 9, 2024, 2:15:52 PM1/9/24
to Zengine Development
Thanks, Wes.
Yesterday, I tried it out using znHttp in my index.js (react). Hope the code below is able to help other developer.
My last question is how to publish. After I published my plugin, I am not able to see the plugin icon in any workspace. 
I tried to click the 'Activate' button in item '3  Privacy Setting'  of publish page (screenshot 1), but I cannot find my plugin in the plugin Marketplace page (screenshot 2)
Any advice for me on how to correctly publish my plugin?
thank you very much.
Allen

=====================
import { znMessage,  znHttp } from '@zenginehq/zengine-sdk';
    let myUrl = '/forms/' + currentForm.formId + '/records';
            znHttp({
                method: 'get',
                url: myUrl,
                params: {
                        limit: 100,
                        page: paginate,
                        sort: 'modified',
                        direction: 'desc',
                        isComplete: true
                }
            })
            .then((response) => {
                let resultAllRecords = response.data.data;
                if (resultAllRecords != undefined) {
                    console.log(resultAllRecords);
                    resultAllRecords.forEach(function (record, index) {
                        .......
                    })
                }
            })
            .then(res => {
                    resolve(res);

            }).catch(() => {
                znMessage('Backend Service Fail', 'error');
            });
screenshot 1.png
screenshot 2.png

Wes

unread,
Jan 10, 2024, 9:55:41 AM1/10/24
to Zengine Development
Only WizeHive plugins will appear in the marketplace. From your screenshot it looks like the plugin was added to the workspace because it appears in the "Added to" section. You may not be able to activate the plugin if you aren't an admin in that workspace. Clicking activate might prompt you to sign in and approve the plugin for offline use. If you are having trouble with this part, please contact support.

Plugins have different injection points based on your plugin code. Your plugin could appear in the header menu or it could appear under the workspace settings screen. See this code in the example plugin for various injection points and compare to your plugin code. https://github.com/ZengineHQ/zengine-frontend-plugin-react/blob/master/example-plugin.json#L6

Chunjen Chen

unread,
Jan 10, 2024, 10:42:54 AM1/10/24
to Zengine Development
Hey Wes, 

Would you please take few minutes to look below the plugin.json code and the screenshot attached. 
Before I contact the support, I would like to confirm that everything is good in development side.

1.plugin.json: 
{
  "icon": "icon-download",
  "views": [
    {
      "type": "fullPage",
      "src": "/index.html"
    }
  ]
}

2. I am an admin for the workspace, The plugin is able to display in the menu bar in Draft Mode but not able to see in Production  (screenshot 1) 

3. The plugin has fully published (screenshot 2)

Thank you very much.
Allen


screenshot 1.png
screenshot 2.png

Wes

unread,
Jan 10, 2024, 11:25:08 AM1/10/24
to Zengine Development
Your code looks fine, especially since it works in draft mode.

You should be able to activate the plugin if you are an admin in the workspace. Pay attention what happens when you click Activate. Check for a popup to authorize if you have an offline backend service. Look for any console errors if nothing seems to happen. Beyond this, I would contact support since it doesn't seem to be a plugin development issue.

Chunjen Chen

unread,
Jan 10, 2024, 2:53:18 PM1/10/24
to Zengine Development
Hey Wes,
"Pay attention what happens when you click Activate. Check for a popup to authorize if you have an offline backend service. Look for any console errors if nothing seems to happen."
I did not see any popup after I clicked Activate, it just jumps to the market page.i don't think i have any offline backend service. and no error message.
Any more advice?

I also submitted a ticket to support team. Unfortunately, Morgan Edwards told me to get back to this forum because all custom development questions are outside of support's wheelhouse. ;-(

Allen

Chunjen Chen

unread,
Feb 1, 2024, 10:19:07 AM2/1/24
to Zengine Development
hey Wes,
I am still stuck on not able to activate my plugin after I am sure  i am an admin in a workspace. the attachment file is the screenshot of the api swagger in which I know i am an admin in a workspace. (We have more than 30 admins in every workspaces in our organization. My owner has helped me temporary remove admin to under twenty. Now I am able to see my record in API /workspaces/{workspace.id}/roles.json.  Unfortunately,  I still see the Activate button is disabled.
Is there anything I can do further to figure out what's going on? 

Thank you very much.
Allen


NotAbleToActivatePlugin.png

Chris M

unread,
Feb 2, 2024, 10:30:54 AM2/2/24
to Zengine Development
Can you please try to activate it from the Marketplace -> Installed View?

If that does not work please open a ticket here  https://wizehive.atlassian.net/servicedesk/customer/portal/1 and we can get in direct contact with you to review your workspace 
Reply all
Reply to author
Forward
0 new messages