Some sapi funtions from p2p api for merchants don't work

Hi. I’m merchant. Support gave me api for p2p and i try to write bot for p2p trading. But some functions works, some not

This is work:

POST /sapi/v1/c2c/ads/getDetailByNo

Code below

import hmac
import time
import hashlib
import requests
from urllib.parse import urlencode

KEY = ""
SECRET = ""
BASE_URL = "https://api.binance.com"

def hashing(query_string):
    return hmac.new(
        SECRET.encode("utf-8"), query_string.encode("utf-8"), hashlib.sha256
    ).hexdigest()


def get_timestamp():
    return int(time.time() * 1000)


def dispatch_request(http_method):
    session = requests.Session()
    session.headers.update(
        {"Content-Type": "application/json;charset=utf-8", "X-MBX-APIKEY": KEY}
    )
    return {
        "GET": session.get,
        "DELETE": session.delete,
        "PUT": session.put,
        "POST": session.post,
    }.get(http_method, "GET")


# used for sending request requires the signature
def send_signed_request(http_method, url_path, payload={}):
    query_string = urlencode(payload, True)
    if query_string:
        query_string = "{}&timestamp={}".format(query_string, get_timestamp())
    else:
        query_string = "timestamp={}".format(get_timestamp())

    url = (
        BASE_URL + url_path + "?" + query_string + "&signature=" + hashing(query_string)
    )
    print("{} {}".format(http_method, url))
    params = {"url": url, "params": {}}
    response = dispatch_request(http_method)(**params)
    return response.json()


# used for sending public data request
def send_public_request(url_path, payload={}):
    query_string = urlencode(payload, True)
    url = BASE_URL + url_path
    if query_string:
        url = url + "?" + query_string
    print("{}".format(url))
    response = dispatch_request("GET")(url=url)
    return response.json()

params = {
    "adsNo":"11378191646449455104"
}

response = send_signed_request("POST", "/sapi/v1/c2c/ads/getDetailByNo", params)
print(response)

But when i try for example to change status of ad. I got an error

POST /sapi/v1/c2c/ads/updateStatus

code below

import hmac
import time
import hashlib
import requests
from urllib.parse import urlencode

KEY = ""
SECRET = ""
BASE_URL = "https://api.binance.com"

def hashing(query_string):
    return hmac.new(
        SECRET.encode("utf-8"), query_string.encode("utf-8"), hashlib.sha256
    ).hexdigest()


def get_timestamp():
    return int(time.time() * 1000)


def dispatch_request(http_method):
    session = requests.Session()
    session.headers.update(
        {"Content-Type": "application/json;charset=utf-8", "X-MBX-APIKEY": KEY}
    )
    return {
        "GET": session.get,
        "DELETE": session.delete,
        "PUT": session.put,
        "POST": session.post,
    }.get(http_method, "GET")


# used for sending request requires the signature
def send_signed_request(http_method, url_path, payload={}):
    query_string = urlencode(payload, True)
    if query_string:
        query_string = "{}&timestamp={}".format(query_string, get_timestamp())
    else:
        query_string = "timestamp={}".format(get_timestamp())

    url = (
        BASE_URL + url_path + "?" + query_string + "&signature=" + hashing(query_string)
    )
    print("{} {}".format(http_method, url))
    params = {"url": url, "params": {}}
    response = dispatch_request(http_method)(**params)
    return response.json()


# used for sending public data request
def send_public_request(url_path, payload={}):
    query_string = urlencode(payload, True)
    url = BASE_URL + url_path
    if query_string:
        url = url + "?" + query_string
    print("{}".format(url))
    response = dispatch_request("GET")(url=url)
    return response.json()

params = {
    "advNos": ["11378191646449455104"],
    "advStatus": 0
}

response = send_signed_request("POST", "/sapi/v1/c2c/ads/updateStatus", params)
print(response)

I got an error

{'code': -1000, 'msg': 'An unknown error occurred while processing the request.'}

Please help. What am I doing wrong?

Looks like you are sending the parameters in your url and also in the body. Remove the parameters from the url and try again. Also try to urlencode the params before sending.

Karagiozis Karagiozis how did you solve this problem, i’m facing similar issue, the solution suggested is not working for me

1 Like

Someone solve this problem for sapi c2c? This error show on every POST request where parameters must be in the body… Binance support doesn’t respond nothing about this issue…

Please help, how did you solve this problem?

i have same issue

you tried the endpoint with postman or insomnia? maybe that can help you to understand hows supossed to work the endpoint

Hello @Karagiozis and everyone, of the things that work, does being able to modify the price of an ads work as a merchant?

If you wish to try I may suggest the below:

Modify the send_signed_request function: Change how you handle the parameters by sending them in the JSON body of the request, rather than in the URL.
def send_signed_request(http_method, url_path, payload={}):
# Create the base query for timestamp
query_string = “timestamp={}”.format(get_timestamp())
signature = hashing(query_string)

# URL without payload parameters
url = f"{BASE_URL}{url_path}?{query_string}&signature={signature}"

# Dispatch the request with the payload in the body
params = {"url": url, "json": payload}  # Note the change from 'params' to 'json'
response = dispatch_request(http_method)(**params)
return response.json()

Ensure Proper Header for JSON Content-Type: Verify that your dispatch function or the request headers are correctly set to handle JSON body:

def dispatch_request(http_method):
session = requests.Session()
session.headers.update(
{“Content-Type”: “application/json;charset=utf-8”, “X-MBX-APIKEY”: KEY}
)
return {
“GET”: session.get,
“DELETE”: session.delete,
“PUT”: session.put,
“POST”: session.post,
}.get(http_method, “GET”)

As suggested in the previous responses, try using tools like Postman to manually make requests. This will allow you to see exactly how the API reacts to different structures of your requests.