1

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:

otoyo
  • 13
  • 2
  • Was dealing with this recently, turned out all I had to do was make a separate API route to call the Notion API that wasn't on the edge runtime, and then call that API route from the original one which is on the edge runtime. This is helpful if you need your original API route to be on the edge runtime like I did to use the @vercel/og library – Neesh Jan 07 '23 at 00:21
  • @Neesh Thank you for the excellent idea! I implemented separate APIs, one is to respond og-image by using @vercel/og on the Edge Runtime, and the other one is to request Notion API and response its response on the normal environment for the proxy from the Edge Runtime. They works well. – otoyo Jan 07 '23 at 07:03

1 Answers1

0

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.

Kopwithit
  • 26
  • 4