How to successfully create a contract using python request lib?
Hello Damler’s
my snadbox is runing against my ledger id
my json api is also runing
but when ever I run a python script to create a contract, the token was specified by authentication using JWT same ledger id used to start sandbox.
under ui/package.json i have added "proxy": "http://localhost:7575"
I am getting the following error:
HTTP service responded: <Response [404]>
{'errors': ['HttpMethod(POST), uri: http://localhost:7575/command/create'], 'status': 404}
Please advise
Thanks,
Hi @KsarCapital, welcome to the forum!
The endpoint for creating new contracts is /v1/create not /command/create which seems to be what your code is using. You can find this and more information in the docs.
Let us know if changing that fixes your issue!
Welcome to the forum @KsarCapital!
I have got a different error:
Cause: spray.json.DeserializationException: Expected JsString([:]:), got: {“entityName”:“Transfer”,“moduleName”:“Main”}’], ‘status’: 400}
Many thanks for the pointer @cocreature,
Hi @KsarCapital, if you look at the linked docs for the /v1/create request, you can see an example of the format for the request body
{
"templateId": "Iou:Iou",
"payload": {
"issuer": "Alice",
"owner": "Alice",
"currency": "USD",
"amount": "999.99",
"observers": []
}
}
There is no entityName and moduleName field. Instead there is a single templateId field with (optional package id, ) module and entity name separated by colons. Are you maybe working based on some old documentation that uses a different format?
sdk-version: 1.3.0
daml 1.2
Here is my request:
return requests.post(
f"{endpoint}/v1/create",
headers = tokenHeader,
json = {
"templateId" : {
"moduleName": "Main",
"entityName": contractName
},
"meta" : {
"ledgerEffectiveTime": epoch # Wall time unsupported on DABL
},
"argument": {
"event": damlDict,
"owner": singatoryParty,
"obs": singatoryParty
}
},
verify=False
)
If you compare the JSON response body in your code to the one in the docs, there are 3 differences:
-
templateIdshould be a single string with module name and entity name separate by:. - You don’t need to set anything in
meta. -
argumentis calledpayload.
If you change those things in your example, you get
{
"templateId" : "Main:{}".format(contractName),
"payload": {
"event": damlDict,
"owner": singatoryParty,
"obs": singatoryParty
}
}
which should hopefully do the trick.
Could you share where you got the format in your code from? This used to be the syntax a few months ago (way before SDK 1.0.0) but there might still be some documentation that we need to update.