N8n API - Post /credentials with openAiApi data schema

I am trying to create an openAiApi credentials using the public rest api. I have made a request to “/api/v1/credentials/schema/” endpoint to retrieve the required values and it only returns “apiKey”. However, when I try to create the credential using the POST on “/api/v1/credentials/”, I get the following error:

{
“message”: “request.body.data does not match allOf schema [subschema 0] with 4 error[s]:,request.body.data does not match allOf schema [subschema 0] with 1 error[s]:,request.body.data requires property “headerName”,request.body.data does not match allOf schema [subschema 1] with 1 error[s]:,request.body.data requires property “headerValue””
}

Any idea what might be the issue?

@afavar the issue is that your request body is missing the headerName and headerValue properties that the n8n API expects for the OpenAI credential type.

The structure should look like this

{
  "name": "Your Credential Name",
  "type": "openAiApi", 
  "data": {
    "apiKey": "your-api-key-here",
    "headerName": "Authorization", 
    "headerValue": "Bearer your-api-key-here" 
  }
}

Let me know if this helps

1 Like

It does work! But the problem is I want to rely on the schema api and the response I got from the api does not mention about these two extra properties as required.

Edit: It does also work if I set the header property to false but I guess omitting it should be also working.

{
  "additionalProperties": false,
  "type": "object",
  "properties": {
    "apiKey": {
      "type": "string"
    },
    "organizationId": {
      "type": "string"
    },
    "url": {
      "type": "string"
    },
    "header": {
      "type": "boolean"
    },
    "headerName": {
      "type": "string"
    },
    "headerValue": {
      "type": "string"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "header": {
            "enum": [
              true
            ]
          }
        }
      },
      "then": {
        "allOf": [
          {
            "required": [
              "headerName"
            ]
          },
          {
            "required": [
              "headerValue"
            ]
          }
        ]
      },
      "else": {
        "allOf": [
          {
            "not": {
              "required": [
                "headerName"
              ]
            }
          },
          {
            "not": {
              "required": [
                "headerValue"
              ]
            }
          }
        ]
      }
    }
  ],
  "required": [
    "apiKey"
  ]
}

@afavar The OpenAI API doesn’t understand the complex “if/then” logic in your schema.

Removing the conditional rules and just listing "apiKey" as the only required field in your schema.

Kindly mark as the solution.

2 Likes

I don’t understand, the schema is returned by the n8n API itself, it is not my schema. Looking at that schema, only required root field is the “apiKey”. However, doing so results with an error upon creating the credential.

api/v1/credentials/schema/openAiApi

Isn’t the point of this endpoint to provide required information for programatic credential creation?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.