Skip to content

Notion API request fails with HTTP 504 Gateway Timeout #311

Closed
@fortran01

Description

@fortran01

See the simplified logs below. I also tried reducing concurrency to 1; see the difference below.

Simplified Debug Log for 504 Gateway Timeout Issue

AxiosError: Request failed with status code 504
    at settle (...)  
    at IncomingMessage.handleStreamEnd (...)  
    at process.processTicksAndRejections (...)  

Error Code: 'ERR_BAD_RESPONSE'

Request Details:

  • Method: POST
  • URL: https://www.notion.so/api/v3/enqueueTask
  • Headers:
    • Accept: application/json, text/plain, /
    • Content-Type: application/json
    • Cookie: token_v2=***
    • User-Agent: axios/1.7.9
    • Accept-Encoding: gzip, compress, deflate, br
  • Request Body: (redacted but relevant structure)
    {
      "task": {
        "eventName": "exportBlock",
        "request": {
          "block": {
            "id": "BLOCK_ID_PLACEHOLDER",
            "spaceId": "SPACE_ID_PLACEHOLDER"
          },
          "exportOptions": {
            "exportType": "markdown",
            "locale": "en",
            "timeZone": "Europe/Vienna"
          },
          "recursive": true
        }
      }
    }

Response Details:

  • Status Code: 504 (Gateway Timeout)
  • Status Message: Gateway Timeout
  • Response Headers:
    • Server: cloudflare
    • CF-Cache-Status: DYNAMIC
    • Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
    • X-Content-Type-Options: nosniff
    • Date: Sat, 22 Feb 2025 15:38:38 GMT

Response Data (HTML):

<html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
</body>
</html>

Environment:

  • Node.js Version: v22.13.1
  • Axios Version: 1.7.9
  • Timeout Setting: 0 (no timeout)

Reduce concurrency=1

diff --git a/index.ts b/index.ts
index f0c15d7..7caad01 100644
--- a/index.ts
+++ b/index.ts
@@ -5,6 +5,10 @@ import axios from 'axios';
 import extract from 'extract-zip';
 import pMap from 'p-map';
 
+// Configure the number of concurrent export tasks
+// Default is 1 to be conservative with API rate limits
+const CONCURRENT_EXPORTS = 1;
+
 const blocks = [
   {
     // Find the page block ID by either:
@@ -97,6 +101,7 @@ function delay(ms: number) {
 
 // Enqueue all export tasks immediately, without waiting for the
 // export tasks to complete
+// Process blocks with configured concurrency to avoid timeouts
 const enqueuedBlocks = await pMap(blocks, async (block) => {
   const {
     data: { taskId },
@@ -137,7 +142,7 @@ const enqueuedBlocks = await pMap(blocks, async (block) => {
     ...block,
     task: task,
   };
-});
+}, { concurrency: CONCURRENT_EXPORTS }); // Control number of concurrent exports
 
 let retries = 0;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions