Code Examples
Ready-to-use code examples in multiple programming languages for integrating with the Weir AI API v1.0.1. These examples demonstrate common patterns and best practices for different use cases.
Quick Start Examples
External API - Generate Access Token
const generateAccessToken = async ( clientId , secretKey ) => {
const credentials = btoa ( ` ${ clientId } : ${ secretKey } ` );
try {
const response = await fetch ( 'https://api.weir.ai/auth/token' , {
method: 'POST' ,
headers: {
'Authorization' : `Basic ${ credentials } ` ,
'Content-Type' : 'application/json'
}
});
if ( ! response . ok ) {
throw new Error ( `HTTP error! status: ${ response . status } ` );
}
const data = await response . json ();
return data . data . accessToken ;
} catch ( error ) {
console . error ( 'Error generating access token:' , error );
throw error ;
}
};
// Usage
const accessToken = await generateAccessToken ( 'your_client_id' , 'your_secret_key' );
console . log ( 'Access Token:' , accessToken );
Console API - Create Team
const createTeam = async ( accessToken , teamName ) => {
try {
const response = await fetch ( 'https://api.weir.ai/org/create/team' , {
method: 'POST' ,
headers: {
'Authorization' : `Bearer ${ accessToken } ` ,
'Content-Type' : 'application/json' ,
'x-source' : 'console'
},
body: JSON . stringify ({ name: teamName })
});
if ( ! response . ok ) {
throw new Error ( `HTTP error! status: ${ response . status } ` );
}
const data = await response . json ();
return data . data ;
} catch ( error ) {
console . error ( 'Error creating team:' , error );
throw error ;
}
};
// Usage
const team = await createTeam ( 'your_access_token' , 'Development Team' );
console . log ( 'Created team:' , team );
Common Patterns
Token Management
class TokenManager {
constructor () {
this . accessToken = null ;
this . refreshToken = null ;
this . tokenExpiresAt = null ;
}
async login ( username , password ) {
const response = await fetch ( 'https://api.weir.ai/auth/login' , {
method: 'POST' ,
headers: { 'Content-Type' : 'application/json' },
body: JSON . stringify ({ username , password })
});
const data = await response . json ();
this . accessToken = data . data . accessToken ;
this . refreshToken = data . data . refreshToken ;
this . tokenExpiresAt = Date . now () + ( data . data . expiresIn * 1000 );
return data ;
}
async refreshToken () {
const response = await fetch ( 'https://api.weir.ai/auth/refresh/token' , {
method: 'POST' ,
headers: { 'Content-Type' : 'application/json' },
body: JSON . stringify ({ refreshToken: this . refreshToken })
});
const data = await response . json ();
this . accessToken = data . data . accessToken ;
this . refreshToken = data . data . refreshToken ;
this . tokenExpiresAt = Date . now () + ( data . data . expiresIn * 1000 );
return data ;
}
async makeRequest ( url , options = {}) {
if ( this . tokenExpiresAt && Date . now () >= this . tokenExpiresAt - 300000 ) {
await this . refreshToken ();
}
return fetch ( url , {
... options ,
headers: {
'Authorization' : `Bearer ${ this . accessToken } ` ,
'Content-Type' : 'application/json' ,
'x-source' : 'console' ,
... options . headers
}
});
}
}
Error Handling
class APIError extends Error {
constructor ( message , code , details , status ) {
super ( message );
this . name = 'APIError' ;
this . code = code ;
this . details = details ;
this . status = status ;
}
}
async function makeAPIRequest ( url , options = {}) {
try {
const response = await fetch ( url , options );
if ( ! response . ok ) {
const errorData = await response . json ();
throw new APIError (
errorData . error ?. message || 'API request failed' ,
errorData . error ?. code || 'UNKNOWN_ERROR' ,
errorData . error ?. details ,
response . status
);
}
return await response . json ();
} catch ( error ) {
if ( error instanceof APIError ) {
throw error ;
}
// Handle network errors
if ( error . name === 'TypeError' && error . message . includes ( 'fetch' )) {
throw new APIError (
'Network error: Unable to connect to API' ,
'NETWORK_ERROR' ,
null ,
0
);
}
throw new APIError (
'Unexpected error occurred' ,
'UNEXPECTED_ERROR' ,
error . message ,
0
);
}
}
Language-Specific Examples
Best Practices
Store credentials securely in environment variables
Implement automatic token refresh
Handle authentication errors gracefully
Use different credentials for different environments
Implement comprehensive error handling
Provide clear error messages to users
Log errors for debugging
Handle network failures gracefully
Always use HTTPS
Validate and sanitize input
Implement proper CORS policies
Follow OWASP security guidelines
Getting Started
Choose Your Language
Select the programming language that best fits your project.
Review Examples
Browse the examples for your chosen language and use case.
Copy and Customize
Copy the relevant examples and customize them for your needs.
Test and Iterate
Test your implementation and iterate based on your requirements.
Pro Tip : Start with the basic examples to understand the API patterns, then use the more complex examples for building complete integrations. All examples include error handling and best practices.