import StellarSdk from 'stellar-sdk';
class ApiClient {
private readonly apiUrl = "api.defindex.io";
private readonly apiKey: string;
constructor(apiKey: string) {
this.apiKey = apiKey;
}
// Helper for POST requests
async postData(endpoint: string, vaultAddress: string, params: Record<string, any>): Promise<any> {
const response = await fetch(`https://${this.apiUrl}/vault/${vaultAddress}/${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify(params)
});
return await response.json();
}
// Helper for GET requests
async getData(endpoint: string, vaultAddress: string, params?: Record<string, any>): Promise<any> {
const url = params
? `https://${this.apiUrl}/vault/${vaultAddress}/${endpoint}?${new URLSearchParams(params).toString()}`
: `https://${this.apiUrl}/vault/${vaultAddress}/${endpoint}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.apiKey}`
}
});
return await response.json();
}
// Submit a signed transaction. POST /send lives at the API root, not under a vault.
async send(signedXdr: string): Promise<any> {
const response = await fetch(`https://${this.apiUrl}/send`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({ xdr: signedXdr })
});
return await response.json();
}
}