API to communicate python with Multichain

+1 vote
Hi,

I'm building python app and I would like to make a transactions and read blocks "data" and control the blockchain through Python app ( Making transaction, modification, adding new stream, reading data (blocks))

could you plz give me simple example os using API of Multichain
asked Nov 8, 2021 by musharraf90

1 Answer

0 votes
 
Best answer
There are a couple of Python libraries available at the bottom of this page, which you can use as examples:

https://www.multichain.com/developers/
answered Nov 8, 2021 by MultiChain
selected Nov 29, 2021 by musharraf90
Thank for your answer. However still not finding exact the answer that I need. I dunno which library to import from pip ? how to make request I need at least simple code example. I saw the link which helps to understand but i dunno how to start ( library to import) just i need simple code to start with
Please see the documentation for this one, which explains everything: https://github.com/coblo/mcrpc
Thank you so much I did it correct ^_^ but I got something diffecult which how to create stream through API ? How to write them in JSON? I looked into the developer page and documentaion and it's not listed ! even one example in python.
You create a stream using the "create" API command, where the first parameter is "stream".
Hi, I tried many ways and don't work such as
mychain.create("type=stream","Musharraf90",'{"restrict":"write,read"}')
I use multichaincli python library
The first parameter should just be "stream", not "type=stream".
I did it and not work too.
mychain = Multichain(rpcuser, rpcpasswd, rpchost, rpcport, chainname)
mychain.create("stream")

The result is :



Error! Status code: 500
Text: {"result":null,"error":{"code":-1,"message":"create \"entity-type\" \"entity-name\" ...\n\nCreates stream, upgrade, filter, variable or library\n\nArguments:\n1. \"entity-type\"                         (string, required) stream\n2. \"stream-name\"                         (string, required) Stream name, if not \"\" should be unique.\n3. open                                  (boolean, required) Allow anyone to publish in this stream\n  or \n3. restrictions                          (object, optional) Stream restrictions\n    {\n      \"restrict\" : \"restrictions\"        (string, optional) Stream restrictions, comma delimited. Possible values: write,read,offchain,onchain\n      \"salted\" : true|false              (boolean, optional) Indicates whether offchain item chunk hashes should be salted\n    }\n4  custom-fields                         (object, optional)  a JSON object with custom fields\n    {...}\n  or \n1. \"entity-type\"                         (string, required) upgrade\n2. \"upgrade-name\"                        (string, required) Upgrade name, if not \"\" should be unique.\n3. open                                  (boolean, required) Should be false\n4. custom-fields                         (object, required) A JSON object with custom fields\n    {\n      \"protocol-version\": version        (numeric, optional) Protocol version to upgrade to\n      \"parameter-name\": value            (numeric, optional) New value for upgradable parameter, one of: \n                                                        target-block-time,\n                                                        maximum-block-size,\n                                                        max-std-tx-size,\n                                                        max-std-op-returns-count,\n                                                        max-std-op-return-size,\n                                                        max-std-op-drops-count,\n                                                        max-std-element-size,\n                                                        anyone-can-connect,\n                                                        anyone-can-send,\n                                                        anyone-can-receive,\n                                                        anyone-can-receiveempty,\n                                                        anyone-can-issue,\n                                                        anyone-can-create,\n                                                        anyone-can-activate\n      \"startblock\": block                (numeric, optional, default 0) Block to apply from \n      ,...\n    }\n  or \n1. \"entity-type\"                         (string, required) txfilter\n2. \"txfilter-name\"                       (string, required) Tx filter name, if not \"\" should be unique.\n3. options                               (object, required) A JSON object with optional filter options\n    {\n      \"for\": \"entity-identifier\"         (string, optional) Asset/stream identifier - one of: create txid, stream reference, stream name.\n        or\n      \"for\": entity-identifier(s),       (array, optional) A JSON array of asset/stream identifiers.\n      \"libraries\": library-identifier(s) (array, optional) A JSON array of library identifiers.\n    }\n4. \"javascript-code\"                     (string, required) JavaScript filter code, see help filters. Example:\n                                                       function filtertransaction()\n                                                       {\n                                                           var tx=getfiltertransaction();\n                                                                                        \n                                                           if (tx.vout.length<1)\n                                                               return \"One output required\";\n                                                       }\n  or \n1. \"entity-type\"                         (string, required) streamfilter\n2. \"streamfilter-name\"                   (string, required) Stream filter name, if not \"\" should be unique.\n3. options                               (object, required) A JSON object with filter options\n    {\n      \"libraries\": library-identifier(s) (array, optional) A JSON array of library identifiers.\n    }\n4. \"javascript-code\"                     (string, required) JavaScript filter code, see help filters. Example:\n                                                       function filterstreamitem()\n                                                       {\n                                                           var item=getfilterstreamitem();\n                                                                                        \n                                                           if (item.keys.length<2)\n                                                               return \"At least two keys required\";\n                                                       }   \n  or \n1. entity-type                           (string, required) variable\n2. \"variable-name\"                       (string, required) Variable name, if not \"\" should be unique.\n3. open                                  (boolean, optional) Should be true\n4. value                                 (any data, optional, default null) Variable value (JSON objects and arrays allowed).\n  or \n1. entity-type                           (string, required) library\n2. \"library-name\"                        (string, required) Library name, if not \"\" should be unique.\n3. options                               (object, required) Library update options\n    {\n      \"updatemode\" : \"update-mode\"       (string, required) Library update mode. Possible values: none, instant, approve\n    }\n4. \"javascript-code\"                     (string, required) JavaScript library code\n\nResult:\n\"transactionid\"                          (string) The transaction id.\n\nExamples:\n> multichain-cli mony create stream test false \n> multichain-cli mony create stream test false '{\"Description\":\"Test stream\"}'\n> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"create\", \"params\": [\"stream\", \"test\", false] }' -H 'content-type: text/plain;' http://127.0.0.1:9704\n"},"id":1}

Json: {'result': None, 'error': {'code': -1, 'message': 'create "entity-type" "entity-name" ...\n\nCreates stream, upgrade, filter, variable or library\n\nArguments:\n1. "entity-type"                         (string, required) stream\n2. "stream-name"                         (string, required) Stream name, if not "" should be unique.\n3. open                                  (boolean, required) Allow anyone to publish in this stream\n  or \n3. restrictions                               
....
... etc
Yes, there is more than one parameter required for the "create" command. Maybe it will be easier for you to start with the command line to try out different API commands, rather than using the Python library? Then error messages will be clearer, etc... Then once you understand what you need to do, you can translate it into Python calls.
...