Created
February 28, 2023 13:59
-
-
Save mydropcrm/0798023a520826260c60c648dbc0dc6a to your computer and use it in GitHub Desktop.
Monobank corporate API registration (/personal/auth/registration)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import base64 | |
import time | |
import ecdsa | |
import hashlib | |
# Docs - https://api.monobank.ua/docs/corporate.html#tag/Avtorizaciya-ta-nalashtuvannya-kompaniyi | |
if __name__ == '__main__': | |
resource = '/personal/auth/registration' | |
url = f"https://api.monobank.ua{resource}" | |
# openssl ecparam -genkey -name secp256k1 -rand /dev/urandom -out priv.key | |
pem_private_key = open('priv.key').read() | |
# openssl ec -in priv.key -pubout > key.pub | |
pem_public_key = open('public.key').read() | |
timestamp = str(time.time()).split('.')[0] | |
# Load key | |
signing_key = ecdsa.SigningKey.from_pem(pem_private_key) | |
# Get X-sign for request | |
data = (timestamp + resource).encode('utf-8') | |
sign = signing_key.sign(data, hashfunc=hashlib.sha256) | |
sign_b64 = base64.b64encode(sign) | |
# logo of your company | |
with open('logo.jpg', "rb") as image_file: | |
logo_encoded = base64.b64encode(image_file.read()) | |
# openssl ec -in priv.key -pubout > key.pub | |
with open('key.pub', "rb") as public_key_file: | |
public_key_encoded = base64.b64encode(public_key_file.read()) | |
request_data = { | |
"pubkey": public_key_encoded.decode(), | |
"name": "Назва компанії", | |
"description": "Опис вашої компанії до 100 символів", | |
"contactPerson": "Ім'я Прізвище", | |
"phone": "380960000001", | |
"email": "[email protected]", | |
"logo": logo_encoded.decode() | |
} | |
headers = { | |
"X-Time": timestamp, | |
"X-Sign": sign_b64, | |
"Content-Type": "application/json" | |
} | |
response = requests.post(url, headers=headers, data=json.dumps(request_data)) | |
print(f"Response status code: {response.status_code}") | |
print(response.json()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!