For Workers & Pages, what is the name of the domain?
none
What is the error message?
Error: Error: Could not resolve authentication method. Expected one of apiEmail, apiKey, apiToken or userServiceKey to be set. Or for one of the “X-Auth-Email”, “X-Auth-Key”, “Authorization” or “X-Auth-User-Service-Key” headers to be explicitly omitted
What is the issue or error you’re encountering
I can get data but not post
What are the steps to reproduce the issue?
Hi!
I use next.js and typescript. I tried with Pages and Workers. I’d like to use RestAPI with API Cloudflare. My ApiToken is D1:modify.
Can you please explain to me how to fix this security error. I understand that there is an extra layer of security when I want to write to my database, but I don’t know where in my code I need to prove my identity to it.
this works for getting:
export async function getContacts() {
const client = new Cloudflare({
apiToken: process.env.API_TOKEN,
});
for await (const queryResult of client.d1.database.query(
${process.env.DATABASE_ID}
,
{
account_id: ${process.env.ACCOUNT_ID}
,
sql: “SELECT * FROM get”,
}
)) {
return queryResult.results;
}
}
and this doesn’t work for posting:
export async function updateContact(id: number) {
const client = new Cloudflare({
apiToken: process.env.API_TOKEN,
});
try {
for await (const queryResult of client.d1.database.query(
${process.env.DATABASE_ID}
,
{
account_id: ${process.env.ACCOUNT_ID}
,
sql: “UPDATE get SET firstName = ?, lastName = ? WHERE id = ?”,
params: [“John”, “Doe”, id.toString()],
},
{
headers: {
Authorization: Bearer ${process.env.API_TOKEN}
,
“Content-Type”: “application/json”,
},
}
)) {
return queryResult.success;
}
} catch (err) {
console.error(“Error:”, err);
}
}