Hey I am trying to create the credential from the outside of n8n through its api
even the api key is correct it is throwing error of unauthorized. Help me in this . Love to hear from you.
(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)
liam
Agosto 8, 2025, 11:05am
2
Welcome to the community.
We will need details to be able to help you with your error. Please include the error message just know what version youâre on and if youâre self-hosted or on a cloud plan
https://docs.n8n.io/api/v1/credentials
{
âstatusâ: âerrorâ,
âmessageâ: âUnauthorizedâ
}
so am sending the json payload like this
const n8nRes = await axios.post(
\`${N8N_API_URL}/rest/credentials\`,
{
name: credentialName,
type: 'gmailOAuth2',
nodesAccess: \[
{
nodeType: 'n8n-nodes-base.gmail',
nodeTypeVersion: 2,
},
\],
data: {
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
accessToken: access_token,
refreshToken: refresh_token,
tokenType: token_type,
expiresIn: expires_in,
scope,
oauthTokenData: {
access_token,
refresh_token,
scope,
token_type,
expiry_date: Date.now() + expires_in \* 1000,
},
},
},
{
headers: {
Authorization: \`Bearer ${N8N_API_KEY}\`,
âContent-Typeâ: âapplication/jsonâ,
},
}
);
am using selfhosted version ( latest)
liam
Agosto 8, 2025, 11:25am
4
Please see these docs
Your auth header has the wrong name and format
Thanks Liam it is working fine.