Search code examples
next.jsvercelnotion-api

Notion API from Vercel Edge Runtime responds 400


From API routes of Next.js on Vercel Edge Runtime, Notion API, eg: Retrieve a database or Query a database, responds 400 invalid_request_url as follows:

{"object":"error","status":400,"code":"invalid_request_url","message":"Invalid request URL."}

From the local environment (next dev, vercel dev), Notion API responds 200 properly.

Codes are as follows:

Retrieve a database

import { NextRequest } from 'next/server'

export const config = { runtime: 'edge' }

async function(req: NextRequest) {
  const url = `https://api.notion.com/v1/databases/${DATABASE_ID}`
  const headers = {
      'Authorization': `Bearer ${NOTION_API_SECRET}`,
      'Notion-Version': '2022-06-28',
      'Content-Type': 'application/json',
    }

  const response = await fetch(url, {
    method: 'GET',
    headers: headers,
  })

  const data = await response.json()
  console.log(JSON.stringify(data))
}

Query a database

import { NextRequest } from 'next/server'

export const config = { runtime: 'edge' }

async function(req: NextRequest) {
  const { searchParams } = new URL(req.url);

  if (!searchParams.has('slug')) {
    throw new Error('No slug in searchParams.')
  }

  const slug = searchParams.get('slug').trim()

  const url = `https://api.notion.com/v1/databases/${DATABASE_ID}/query`
  const body = JSON.stringify({
      filter: {
        and: [
          { property: 'Published', checkbox: { equals: true } },
          { property: 'Slug', rich_text: { equals: slug } },
        ],
      },
    })
  const headers = {
      'Authorization': `Bearer ${NOTION_API_SECRET}`,
      'Notion-Version': '2022-06-28',
      'Content-Type': 'application/json',
    }

  const response = await fetch(url, {
    method: 'POST',
    body: body,
    headers: headers,
  })

  const data = await response.json()
  console.log(JSON.stringify(data))
}

NOTION_API_SECRET and DATABASE_ID are set properly.

200 is expected but not.

Things I tried are as follows:


Solution

  • I am having the same issue using the node package, I switched my function from edge back to normal and it is working when deployed.

    export default async function handler(req, res) {
      const response = await getPosts();
    
      const scheme = response.scheme;
      const items = response.items;
      res.status(200).json({
        scheme,
        items,
      })
    }
    

    getPosts() definition

    import { Client, isFullDatabase } from "@notionhq/client";
    import { type } from "os";
    const notion = new Client({
      auth: process.env.NOTION_SECRET,
    });
    
    const processDatabase = (database) => {
      const notionDatabase = { scheme: "test", items: [] };
      database.results.forEach((result) => {
        const properties = result.properties;
        const row = {
          id: result.id,
          data: {
            name: properties.Name.title[0].plain_text,
            author: properties.Author.rich_text[0].plain_text,
            status: properties.Status.multi_select[0].name,
            start_date: properties.Start.date?.start,
            end_date: properties.End.date?.start,
          },
        };
        notionDatabase.items.push(row);
      });
      return notionDatabase;
    };
    export async function getPosts() {
      const response = await notion.databases.query({
        database_id: "",
        sorts: [
          {
            property: "Status",
            direction: "ascending",
          },
        ],
      });
      console.log(response);
      return processDatabase(response);
    }
    

    My edge function last worked 34 days ago on a deployment to vercel. My next deployment was 26 days ago, no longer working and returning 500. All that appears to have changed from my build logs was the Vercel CLI verison from 28.6.0 to 28.8.0, not sure that would even matter.