N8n credential creation on its ui through n8n API


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.)

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)

Please see these docs

Your auth header has the wrong name and format

Thanks Liam it is working fine.