Regex’s to validate API Key and Org ID format?

Hi,

Are there any ‘official’ regular expressions for the API key string and the Organisation ID string formats?

To save pointless usage of the api endpoint, I would like to do some simple pre-validation in my functions before attempting a connection/authentication.

I can’t find this mentioned in the documentation.

Any help will be appreciated.

Thanks
M

(‘Moved’ from ‘API Feedback’)…

Hey! We don’t have anything official to do this right now but it’s a good suggestion. Can’t promise we will have anything soon but you can ask ChatGPT to write the regex for you and it should be pretty close to what you need for something like this.

1 Like

Thanks @logankilpatrick

I did ask ChatGPT first :grinning:

But not knowing the official specification/format for each; I wasn’t sure I could 100% trust it’s responses.

I’ll keep an eye out in the documentation in the coming months.

Thanks.
M

For reference, this is the output ChatGPT gave me:

function isValidFormat(apiKey) {
    // Check if it's a string, has some length, and starts with 'sk'
    return typeof apiKey === 'string' && apiKey.length > 0 && apiKey.startsWith('sk');
}

var apiKey = 'YOUR_API_KEY_HERE'; // Replace with your OpenAI key
console.log(isValidFormat(apiKey) ? 'Key format looks good' : 'Key format is incorrect');

PS: I asked for the starts with “sk” check.