Hello. I am trying to use Advanced Trade API to trade futures.
import os
import uuid
import time
import hmac
import json
import math
import base64
import joblib
import hashlib
import requests
import schedule
import warnings
import threading
import numpy as np
import pandas as pd
import urllib.parse
from datetime import datetime
from coinbase import jwt_generator
Your API key and secret
api_key = ‘(key)’
api_secret = ‘-----BEGIN EC PRIVATE KEY-----\n-----END EC PRIVATE KEY-----\n’
def get_futures_products():
request_method = “GET”
request_path = “/api/v3/brokerage/products”
# Ensure JWT is generated right before the request
jwt_uri = jwt_generator.format_jwt_uri(request_method, request_path)
jwt_token = jwt_generator.build_rest_jwt(jwt_uri, api_key, api_secret)
headers = {
"Authorization": f"Bearer {jwt_token}",
"Content-Type": "application/json"
}
full_url = "https://api.coinbase.com" + request_path
response = requests.get(full_url, headers=headers)
print(f"Response status code: {response.status_code}")
print(f"Response text: {response.text}")
if response.status_code == 200:
try:
products_data = response.json()
# Write the response data to symbls.txt file
with open("symbls.txt", "w") as file:
json.dump(products_data, file, indent=4)
return products_data
except json.JSONDecodeError:
print("Failed to decode JSON from response")
return None
else:
print("Failed to authenticate or access the resource")
return None
if name == “main”:
print(“Retrieving futures products…”)
get_futures_products()
As you see, I have a working script to get the product info for spot products, however when I use the provided url from the documentation for futures: “https://api.coinbase.com/api/v3/brokerage/products?product_type=FUTURE” this isn’t working? I just receive a 401 code with an “Unauthorized or cannot access resource” error.
It’s a bit irritating, seeming as it’s directly from the documentation but doesn’t seem to work. But maybe I am missing something so please let me know.
While I am also on the topic, can I just get a quick confirmation that I can actually place futures trades and operate futures with this API? I’m switching from KuCoin, and I specifically need to find out the Coinbase equivalent of Multiplier/lotSize/initialMargin, so for futures product endpoint not to work, it’s just making things more confusing.
Thank you for the assistance.