Package: kin-sdk-v2
Kin Python SDKThe Kin Python SDK enables developers use Kin inside their backend servers. It contains support for blockchain actions
such as creating accounts and sending payments, as well a webhook handler class to assist with implementing Agora webhooks.RequirementsPython 3.6 or higherInstallationpip install kin-sdk-v2OverviewThe SDK contains two main components: theClientand theWebhookHandler. TheClientis used for blockchain
actions, such as creating accounts sending payments, while theWebhookHandleris meant for developers who wish to make
use of Agora Webhooks. It is recommended that developers read thewebsite documentationprior to using this SDK.ClientThe main component of this library is theClientclass, which facilitates access to the Kin blockchain.InitializationAt a minimum, the client needs to be instantiated with anEnvironment.fromagora.clientimportClient,Environmentclient=Client(Environment.TEST)Apps withregisteredapp indexes should initialize the client with their index:fromagora.clientimportClient,Environmentclient=Client(Environment.TEST,app_index=1)UsageThe following outlines some example usages of the client. See thedocumentationfor more details.Create an AccountThecreate_accountmethod creates an account with the provided private key.To create a new account, first generate a new private key, then submit it usingcreate_account:fromagora.clientimportClient,Environmentfromagora.keysimportPrivateKeyclient=Client(Environment.TEST,app_index=1)private_key=PrivateKey.random()client.create_account(private_key)Get a TransactionTheget_transactionmethod gets transaction data by transaction id.fromagora.clientimportClient,Environmentclient=Client(Environment.TEST,app_index=1)# tx_id is either a 32-byte Stellar transaction hash or a 64-byte Solana transaction signaturetx_id=b'<txid>'transaction_data=client.get_transaction(tx_id)Get an Account BalanceTheget_balancemethod gets the balance of the provided account, inquarks.fromagora.keysimportPrivateKeyfromagora.clientimportClient,Environmentclient=Client(Environment.TEST,app_index=1)public_key=PrivateKey.random().public_keybalance=client.get_balance(public_key)Submit a PaymentThesubmit_paymentmethod submits the provided payment to Agora.fromagora.clientimportClient,Environmentfromagora.keysimportPrivateKey,PublicKeyfromagora.modelimportPayment,TransactionTypefromagora.utilsimportkin_to_quarksclient=Client(Environment.TEST,app_index=1)sender=PrivateKey.from_string('SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')dest=PublicKey.from_string('GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')payment=Payment(sender,dest,TransactionType.EARN,kin_to_quarks("1"))tx_hash=client.submit_payment(payment)Submit an Earn BatchThesubmit_earn_batchmethod submits a batch of earns to Agora from a single account. It batches the earns into fewer
transactions where possible and submits as many transactions as necessary to submit all the earns.fromagora.clientimportClient,Environmentfromagora.keysimportPublicKey,PrivateKeyfromagora.modelimportEarn,EarnBatchfromagora.utilsimportkin_to_quarksclient=Client(Environment.TEST,app_index=1)sender=PrivateKey.from_string('SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')dest1=PublicKey.from_string('GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1')dest2=PublicKey.from_string('GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX2')# Send two earns of 1 Kin eachearns=[Earn(dest1,kin_to_quarks("1")),Earn(dest2,100000),# also equivalent to 1 Kin...]batch=EarnBatch(sender,earns)batch_earn_result=client.submit_earn_batch(batch)ExamplesA few examples for creating an account and different ways of submitting payments and batched earns can be found inexamples/client.Webhook HandlerTheWebhookHandlerclass is designed to assist developers with implementing theAgora webhooks.Only apps that have been assigned anapp indexcan make use of Agora webhooks.InitializationTheWebhookHandlermust be instantiated with the app's configuredwebhook secret.fromagora.clientimportEnvironmentfromagora.webhook.handlerimportWebhookHandlerwebhook_handler=WebhookHandler(Environment.TEST,'mysecret')UsageCurrently,WebhookHandlercontains support for the following webhooks:Events, withhandle_eventsSign Transaction, withhandle_sign_transactionEvents WebhookTo use theWebhookHandlerwith the Events webhook, developers should define a function that accepts a list of events and processes them in some way:fromtypingimportListfromagora.webhook.eventsimportEventdefprocess_events(events:List[Event])->None:# some processing logicreturnThis function can be used withWebhookHandler.handle_eventsinside your events endpoint logic as follows:fromtypingimportListfromagora.clientimportEnvironmentfromagora.webhookimportWebhookHandler,AGORA_HMAC_HEADERfromagora.webhook.eventsimportEventwebhook_handler=WebhookHandler(Environment.TEST,'mysecret')defprocess_events(events:List[Event])->None:# some processing logicreturn# This will vary depending on which framework is used.defevents_endpoint_func(request):status_code,request_body=webhook_handler.handle_events(process_events,request.headers.get(AGORA_HMAC_HEADER),request.body,)# respond using provided status_code and request_bodySign Transaction WebhookThe sign transaction webhook is used to sign Kin 3 transactions with a whitelisted Kin 3 account to remove fees. On Kin 4, the webhook can be used to simply approve or reject transactions submitted by mobile clients.To use theWebhookHandlerwith the Sign Transaction webhook, developers should define a function that accepts a sign transaction request and response object and verifies the request in some way and modifies the response object as needed:fromagora.webhook.sign_transactionimportSignTransactionRequest,SignTransactionResponsedefverify_request(req:SignTransactionRequest,resp:SignTransactionResponse)->None:# verify the transaction inside `req`, and modify `resp` as needed.returnThis function can be used withWebhookHandler.sign_transactioninside your sign transaction endpoint logic as follows:fromagora.clientimportEnvironmentfromagora.webhookimportWebhookHandler,AGORA_HMAC_HEADERfromagora.webhook.sign_transactionimportSignTransactionRequest,SignTransactionResponsewebhook_handler=WebhookHandler(Environment.TEST,'mysecret')defverify_request(req:SignTransactionRequest,resp:SignTransactionResponse)->None:# verify the transaction inside `req`, and modify `resp` as needed (e.g. by calling `sign`).return# This will vary depending on which framework is used.defsign_tx_endpoint_func(request):status_code,request_body=webhook_handler.handle_sign_transaction(verify_request,request.headers.get(AGORA_HMAC_HEADER),request.body,)# respond using provided status_code and request_bodyExample CodeA simple example Flask server implementing both the Events and Sign Transaction webhooks can be found inexamples/webhook/app.py. To run it, first install all required dependencies (it is recommended that you use a virtual environment):make deps
make deps-devNext, run it as follows from the root directory (it will run on port 8080):export WEBHOOK_SECRET=yoursecrethere
export WEBHOOK_SEED=SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

python -m examples.webhook.appAPI ReferenceThe API reference can be foundhere.