OKEX Exchange API: Differences Between V1 and V3 Versions (Solved)

·

When fetching Bitcoin price data from OKEX Exchange, you might encounter the error:
"ValueError: If using all scalar values, you must pass an index."

This issue isn’t caused by your code but by the API response format. Here’s how to resolve it and understand the differences between V1 and V3 APIs.


Root Cause of the Error

The error occurs because:


Solutions

Solution 1: Use V1 API

Simplicity is key—switch to the V1 API if you don’t need advanced features.

url = 'https://www.okex.com/api/v1/ticker.do?symbol=%s' % symbol  
df = pd.DataFrame(json_data, dtype='float', index=[0])  

Solution 2: Adapt V3 API Code

Modify your DataFrame construction for V3 responses by:

  1. Removing dtype='float' (to handle mixed data types).
  2. Adding index=[0] to avoid the scalar-values error.

    url = 'https://www.okex.com/api/spot/v3/instruments/%s/ticker' % symbol  
    df = pd.DataFrame(json_data, index=[0])  

Key Differences Between V1 and V3 APIs

| Feature | V1 API | V3 API |
|------------------|----------------------------------|----------------------------------|
| Data Types | Numeric only | Mixed (numbers + strings) |
| Endpoint | /api/v1/ticker.do | /api/spot/v3/instruments/... |
| Flexibility | Limited | More structured responses |

👉 Explore OKEX API documentation for advanced use cases.


FAQs

Q1: Why does V3 API return strings instead of numbers?

A: V3 provides richer metadata (e.g., timestamps as strings), requiring explicit type handling.

Q2: Can I force V3 data into a float format?

A: No—clean the data first (e.g., extract numeric fields) or use Solution 1.

Q3: Which API version is better for real-time trading?

A: V3 offers more granular data, but V1 is simpler for basic price tracking.


Best Practices

Need help? 👉 Check OKEX’s developer resources for updates.

Originally documented on November 25, 2018.