-
Notifications
You must be signed in to change notification settings - Fork 30
Open
Description
I cannot use this sdk to get candles.
Doing this:
const request = {
productId: "DOGE-USDC",
start: "1743800924",
end: "1743804524",
granularity: "FIVE_MINUTE",
limit: 300,
};
const response = await productService.getProductCandles(request);
Throws the error 400 Coinbase Invalid Request Error: Request failed with status code 400 granularity argument is invalid
I verified that the url it generates is https://api.coinbase.com/api/v3/brokerage/products/DOGE-USDC/candles?start=1743800924&end=1743804524&granularity=FIVE_MINUTE&limit=300.
In contrast, I am able to get it without using this library like this:
import fetch from 'node-fetch';
import crypto from 'crypto';
import jwt from 'jsonwebtoken';
function buildJWT(uri: string) {
const privateKey = process.env.COINBASE_API_SECRET;
if (!privateKey) {
throw new Error("Missing secret");
}
const payload = {
sub: process.env.COINBASE_API_KEY,
iss: 'cdp',
nbf: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 120,
uri: uri,
};
const token = jwt.sign(payload, privateKey, {
algorithm: 'ES256',
header: {
kid: process.env.COINBASE_API_KEY,
nonce: crypto.randomBytes(8).toString('hex'),
} as any,
});
return token;
}
async function fetchCandles(productId: string, start: number, end: number) {
const uri = `GET api.coinbase.com/api/v3/brokerage/products/${productId}/candles`;
const jwtToken = buildJWT(uri);
const params = new URLSearchParams({
start: String(start),
end: String(end),
granularity: 'FIVE_MINUTE',
limit: '300',
});
const url = `https://api.coinbase.com/api/v3/brokerage/products/${productId}/candles?${params.toString()}`;
const headers = {
'Authorization': `Bearer ${jwtToken}`,
'Content-Type': 'application/json',
};
const response = await fetch(url, { method: 'GET', headers });
console.log('Status:', response.status);
console.log('URL:', response.url);
const body = await response.text();
console.log('Response:', body);
}
router.get("/test", (req: express.Request, res: express.Response) => {
const now = Math.floor(Date.now() / 1000);
const start = now - 3600;
const end = now;
fetchCandles('DOGE-USDC', start, end).catch(console.error);
res.send("Done")
});
It seems that others have had this issue when their request if not formatted perfectly.
I think there might be something in the way that requests are being made that the products/${productId}/candles endpoint does not like.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels