Use ONLYOFFICE macro to generate an essay with ChatGPT

21 February 2023 By Serge 0 368

AI technology has significantly improved over several years and become an essential part of our daily lives. Today we will incorporate this high-tech functionality into our document writing routine. In this blog post, we will show you how to build a macro that will generate an entire essay with ChatGPT API.

Use ONLYOFFICE macro to generate an essay with ChatGPT

About ChatGPT

ChatGPT is an innovative AI chatbot developed by OpenAI that can converse naturally. Its dialogue design allows it to respond to follow-up questions, recognize its errors, generate text, and dispute incorrect statements. The model is trained to follow the instruction in a prompt and provide a detailed response.

OpenAI provides a free trial API that facilitates integrating into various applications. For convenience purposes, we will get access to OpenAI API through the RapidAPI platform. To connect to the  OpenAI API directly, visit the official documentation page.

About RapidAPI

RapidAPI is a comprehensive platform for developers to discover, connect to, and manage APIs. It contains a directory of over 10,000 APIs, including popular ones like Google Maps, Twilio, Stripe, and YouTube. Users can search for APIs, try out free trials, and subscribe to APIs and services. RapidAPI also offers a dashboard to monitor API subscriptions, usage, and performance, as well as access support. Additionally, RapidAPI enables developers to create their APIs and monetize them.

Forming the API request

In our project, we incorporated the You Chat GPT API by FB/florianbreut. Currently, it’s offers a freemium plan that starts with a basic package of 100 requests a month. It will be sufficient to test our macro.

The interface of the RapidAPI platform generates request code snippets in various languages. In our case choices will be JavaScriot and a fetch function:

Use ONLYOFFICE macro to generate an essay with ChatGPT

Also, we can tweak the request parameters. In particular, we can change the request body. The API offers 4 request options, depending on the OpenAI model:

  1. Write Code
  2. Explain Anything
  3. Time Response Constraint
  4. Formatted Answer

The Formatted Answer option is perfectly suited for essay generation. So we pick it and change the max_response_time parameter to 20 seconds. This will give the OpenAI more time to generate the answer:

Use ONLYOFFICE macro to generate an essay with ChatGPT

Building the macro

Now let’s incorporate this fetch request into our macro!

First, we create a global variable that will store the response:

Then we add the request options and set the topic of the essay by changing the question parameter in the request body:

After that, we define the insertEssay function. This function contains ONLYOFFICE API methods that insert the generated essay into the document.

First, we target the active document and create a paragraph where the text will be added:

Then we add the generated essay to the paragraph and insert it with the InsertContent method.  We also pass in the “KeepTextOnly” parameter to prevent any possible formatting issues:

We also incorporate the handleFetchResponse function. It extracts the answer from the response and fires the insertEssay function to insert it into the document:

And we make a fetch request to the server:

The entire macro code is the following:

Now let’s run our macro and see how it works!


  • This macro is only operational in the online version of the editors.
  • To get 100 free requests, subscribe to You Chat GPT API and use your RapidAPI key.
  • It takes approximately 30 seconds to process the request before inserting it into the document.
  • Press the space to insert the text.

We hope that this macro will bring new fascinating AI technologies into your workflow and alleviate tedious routine tasks. We urge developers to take advantage of the possibilities that our API methods offer and build their macros. Our macros are written in JavaScript, making them highly functional and adaptive.

If you have any inquiries or suggestions, please don’t hesitate to reach out. We are open to hearing your ideas and are eager to collaborate with you. Best of luck in your exploratory endeavors!