-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_api_key.js
40 lines (33 loc) · 1.24 KB
/
test_api_key.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { ClarifaiStub, grpc } = require('clarifai-nodejs-grpc');
// Initialize the gRPC client
const stub = ClarifaiStub.grpc();
const metadata = new grpc.Metadata();
// Clean the API key by removing whitespace and any non-alphanumeric characters
let apiKey = process.env.new_clarifai_key || '';
apiKey = apiKey.trim().replace(/[^a-zA-Z0-9]/g, '');
console.log('Testing API key:', apiKey ? `${apiKey.substring(0, 8)}...` : 'Not found');
// Set the cleaned API key in metadata
metadata.set('authorization', `Key ${apiKey}`);
// Simple test to list models
stub.ListModels(
{
per_page: 1 // Just get one model to verify authentication
},
metadata,
(err, response) => {
if (err) {
console.error('Error testing API key:', err);
process.exit(1);
}
if (response.status.code !== 10000) {
console.error('API test failed:', response.status.description);
process.exit(1);
}
console.log('API key test successful!');
console.log('Response status:', response.status);
if (response.models && response.models.length > 0) {
console.log('Successfully retrieved model:', response.models[0].id);
}
process.exit(0);
}
);