# Official SDK

### **📚 GitHub Demo**

You can find the official SDK and usage examples here:\
🌐 [**Coinlocally SDK on GitHub**](https://github.com/exchange2021)

***

### 🔐 Signature Generation Example (Java)

```java
javaCopyEdit/**
 * Generate request signature
 */
private String toSign(String timestamp, String method, String requestPath,
                      String queryString, String body, String secretKey) throws Exception {
    String preHash = preHash(timestamp, method, requestPath, queryString, body);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(secretKeySpec);
    return Hex.encodeHexString(mac.doFinal(preHash.getBytes("UTF-8")));
}

/**
 * Construct the string to be signed
 */
private String preHash(String timestamp, String method, String requestPath,
                       String queryString, String body) {
    StringBuilder preHash = new StringBuilder()
        .append(timestamp)
        .append(method.toUpperCase())
        .append(requestPath);

    if (StringUtils.isNotEmpty(queryString)) {
        preHash.append("?").append(queryString);
    }
    if (StringUtils.isNotEmpty(body)) {
        preHash.append(body);
    }
    return preHash.toString();
}

/**
 * Extract query string from request URL
 */
private String queryString(ServerHttpRequest request) {
    String url = request.getURI().toString();
    return url.contains("?") ? url.substring(url.lastIndexOf("?") + 1) : "";
}
```

***

### 📦 Create Order Examples (By Language)

#### **✅ Java (Using OkHttp)**

```java
javaCopyEditOkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"symbol\":\"BTCUSDT\",\"volume\":1,\"side\":\"BUY\",\"type\":\"LIMIT\",\"price\":10000}");
Request request = new Request.Builder()
  .url("https://openapi.coinlocally.com/sapi/v2/order")
  .post(body)
  .addHeader("X-CH-APIKEY", "Your API key")
  .addHeader("X-CH-TS", "1596543296058")
  .addHeader("X-CH-SIGN", "encrypted-signature")
  .addHeader("Content-Type", "application/json")
  .build();
Response response = client.newCall(request).execute();
```

#### **🐍 Python**

```python
pythonCopyEditimport requests

url = "https://openapi.coinlocally.com/sapi/v2/order"
headers = {
  "X-CH-APIKEY": "Your API key",
  "X-CH-TS": "1596543881257",
  "X-CH-SIGN": "encrypted-signature",
  "Content-Type": "application/json"
}
payload = {
  "symbol": "BTCUSDT",
  "volume": 1,
  "side": "BUY",
  "type": "LIMIT",
  "price": 10000
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
```

#### **🌐 Go**

```go
goCopyEditpackage main

import (
  "net/http"
  "strings"
  "fmt"
  "io/ioutil"
)

func main() {
  payload := strings.NewReader(`{"symbol":"BTCUSDT","volume":1,"side":"BUY","type":"LIMIT","price":10000}`)
  req, _ := http.NewRequest("POST", "https://openapi.coinlocally.com/sapi/v2/order", payload)

  req.Header.Add("X-CH-APIKEY", "Your API key")
  req.Header.Add("X-CH-TS", "1596543881257")
  req.Header.Add("X-CH-SIGN", "encrypted-signature")
  req.Header.Add("Content-Type", "application/json")

  res, _ := http.DefaultClient.Do(req)
  defer res.Body.Close()
  body, _ := ioutil.ReadAll(res.Body)
  fmt.Println(string(body))
}
```

#### **📘 PHP**

```php
phpCopyEdit<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://openapi.coinlocally.com/sapi/v2/order');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setHeader([
  'X-CH-APIKEY' => 'Your API key',
  'X-CH-TS' => '1596543881257',
  'X-CH-SIGN' => 'encrypted-signature',
  'Content-Type' => 'application/json'
]);
$request->setBody('{"symbol":"BTCUSDT","volume":1,"side":"BUY","type":"LIMIT","price":10000}');

try {
  $response = $request->send();
  echo $response->getBody();
} catch (HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}
?>
```

#### **⚙️ Node.js**

```js
jsCopyEditconst request = require('request');

const options = {
  method: 'POST',
  url: 'https://openapi.coinlocally.com/sapi/v2/order',
  headers: {
    'X-CH-APIKEY': 'Your API key',
    'X-CH-TS': '1596543881257',
    'X-CH-SIGN': 'encrypted-signature',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    symbol: "BTCUSDT",
    volume: 1,
    side: "BUY",
    type: "LIMIT",
    price: 10000
  })
};

request(options, (error, response) => {
  if (error) throw new Error(error);
  console.log(response.body);
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coinlocally.com/official-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
