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:
- Requests to other API, like https://jsonplaceholder.typicode.com/, are OK
- Trying
export const config = { runtime: 'experimental-edge' }
but 400 - Trying Edge Runtime CLI in the local was OK(200)