Gates.io

Gates.io

Cryptocurrency

Blockchain Assets Exchange

Visit API

📚 Documentation & Examples

Everything you need to integrate with Gates.io

🚀 Quick Start Examples

Gates.io Javascript Examplejavascript
// Gates.io API Example
const response = await fetch('https://www.gate.io/api2', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
});

const data = await response.json();
console.log(data);

Using the Gate.io API with JavaScript

Gate.io is a cryptocurrency exchange that offers an API for developers to interact with the platform programmatically. In this guide, we’ll explore how to use this API with JavaScript.

Getting Started

First, you’ll need to obtain an API key and secret from Gate.io. To do this, log in to your account and navigate to the API Management page. Generate a new API key with the desired permissions and save both the API key and secret somewhere safe.

Endpoint URL

The API endpoints for Gate.io are accessed through the following URL:

https://api.gateio.ws/api/v4/spot/

Example API Calls

Now let’s look at some example API calls you can make with JavaScript.

Authentication

Before making any authenticated requests, you’ll need to include the API key and signature in the request headers. Here’s how to create the necessary headers for an API request:

const apiKey = '[YOUR_API_KEY]';
const secret = '[YOUR_SECRET]';

const getSignature = (nonce, method, url, data) => {
  const message = `${nonce}${method.toUpperCase()}${url}${JSON.stringify(data)}`;
  const hash = CryptoJS.HmacSHA512(message, secret);
  const signature = CryptoJS.enc.Base64.stringify(hash);
  return signature;
};

const getHeaders = (method, url, data) => {
  const nonce = Date.now().toString();
  const signature = getSignature(nonce, method, url, data);
  return {
    'Content-Type': 'application/json',
    'KEY': apiKey,
    'Timestamp': nonce,
    'SIGN': signature,
  };
};

Get Tickers

This API call retrieves ticker data for all trading pairs.

const url = `https://api.gateio.ws/api/v4/spot/tickers`;
const headers = getHeaders('GET', '/api/v4/spot/tickers');
fetch(url, {
  headers,
  method: 'GET',
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error));

Get Order Book

This API call retrieves the order book for a specific trading pair.

const currencyPair = 'BTC_USDT';
const url = `https://api.gateio.ws/api/v4/spot/order_book?currency_pair=${currencyPair}`;
const headers = getHeaders('GET', `/api/v4/spot/order_book?currency_pair=${currencyPair}`);
fetch(url, {
  headers,
  method: 'GET',
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error));

Place Order

This API call places a limit buy order for the specified trading pair.

const currencyPair = 'BTC_USDT';
const side = 'buy';
const price = '100';
const amount = '1';
const url = `https://api.gateio.ws/api/v4/spot/orders?currency_pair=${currencyPair}&side=${side}&price=${price}&amount=${amount}`;
const headers = getHeaders('POST', '/api/v4/spot/orders', {
  currency_pair: currencyPair,
  side: side,
  price: price,
  amount: amount,
});
fetch(url, {
  headers,
  method: 'POST',
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error));

Conclusion

In this guide, we’ve covered how to use the Gate.io API with JavaScript to retrieve ticker and order book data, as well as place orders. By using the code examples provided here, you’ll be able to start building your own cryptocurrency trading bot or application that integrates with the Gate.io exchange.

📊 30-Day Uptime History

Daily uptime tracking showing online vs offline minutes

Jun 4Jun 6Jun 8Jun 10Jun 12Jun 14Jun 16Jun 18Jun 20Jun 22Jun 24Jun 26Jun 28Jun 30Jul 304008001440Minutes
Online
Offline

Related APIs in Cryptocurrency