Skip to content

Commit a455aae

Browse files
docs(getting-started): add Python tab to Create an Account tutorial (#170)
Signed-off-by: EMerchant90 <[email protected]> Signed-off-by: Ejaz Merchant <[email protected]> Co-authored-by: krystal <[email protected]>
1 parent 7d0b8ad commit a455aae

File tree

4 files changed

+743
-12
lines changed

4 files changed

+743
-12
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.env
22
.env.example
33
*.bak
4-
.idea
4+
.venv/
5+
__pycache__/
6+
.idea

getting-started-hedera-native-developers/create-a-token.md

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,85 @@ go mod tidy
165165
```
166166
{% endcode %}
167167
{% endtab %}
168+
169+
{% tab title="Python" %}
170+
**Before you start:** Ensure you have Python 3.10+ installed on your machine. Run this command to verify.
171+
172+
```bash
173+
python --version
174+
```
175+
176+
If the `python --version` command is not found or shows a version lower than 3.10, install or upgrade Python from [Python Install](https://www.python.org/downloads/).
177+
178+
**Note:** On some systems, you may need to use `python3` instead of `python` for initial setup commands. If `python --version` doesn't work, try `python3 --version` and use `python3` for the virtual environment creation. After activating the virtual environment, always use `python` for all commands.
179+
180+
Open your terminal and create a working directory for your Hedera project. Then navigate into the new directory:
181+
182+
```bash
183+
mkdir hedera-examples && cd hedera-examples
184+
```
185+
186+
**Verify Python and pip:** Ensure you have Python 3.10+ and pip installed on your machine. Run these commands to check:
187+
188+
```bash
189+
python --version
190+
```
191+
192+
```bash
193+
python -m pip --version
194+
```
195+
196+
Create a virtual environment to isolate your project dependencies (Python best practice):
197+
198+
```bash
199+
python -m venv .venv
200+
```
201+
202+
Activate the virtual environment to use the isolated Python installation:
203+
204+
{% tabs %}
205+
{% tab title="Mac/Linux" %}
206+
```bash
207+
source .venv/bin/activate
208+
```
209+
{% endtab %}
210+
211+
{% tab title="Windows" %}
212+
```bash
213+
.venv\Scripts\activate
214+
```
215+
{% endtab %}
216+
{% endtabs %}
217+
218+
Upgrade pip to ensure you have the latest package installer (recommended):
219+
220+
```bash
221+
python -m pip install --upgrade pip
222+
```
223+
224+
Install the [Python SDK](https://github.com/hiero-ledger/hiero-sdk-python):
225+
226+
```bash
227+
python -m pip install hiero_sdk_python
228+
```
229+
230+
Create a file named `CreateTokenDemo.py` and add the following imports:
231+
232+
```python
233+
import os
234+
import time
235+
import requests
236+
237+
from hiero_sdk_python import (
238+
Client,
239+
AccountId,
240+
PrivateKey,
241+
TokenCreateTransaction,
242+
TokenType,
243+
SupplyType,
244+
)
245+
```
246+
{% endtab %}
168247
{% endtabs %}
169248

170249
***
@@ -221,6 +300,18 @@ client := hedera.ClientForTestnet()
221300
client.SetOperator(operatorId, operatorKey)
222301
```
223302
{% endtab %}
303+
304+
{% tab title="Python" %}
305+
```python
306+
# Load your operator credentials
307+
operatorId = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
308+
operatorKey = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
309+
310+
# Initialize your testnet client and set operator
311+
client = Client()
312+
client.set_operator(operatorId, operatorKey)
313+
```
314+
{% endtab %}
224315
{% endtabs %}
225316

226317
***
@@ -259,6 +350,14 @@ supplyKey, _ := hedera.PrivateKeyGenerateEcdsa() // can mint/burn
259350
adminKey := supplyKey // can update/delete (reuse for simplicity)
260351
```
261352
{% endtab %}
353+
354+
{% tab title="Python" %}
355+
```python
356+
# Generate keys that control your token
357+
supply_key = PrivateKey.generate_ecdsa() # can mint/burn
358+
admin_key = supply_key # can update/delete (reuse for simplicity)
359+
```
360+
{% endtab %}
262361
{% endtabs %}
263362

264363
**‼️ Security reminder**: Keep your private keys secure - anyone with access can control your token.
@@ -347,6 +446,31 @@ tokenId := *receipt.TokenID
347446
fmt.Printf("Token created: %s\n", tokenId.String())
348447
```
349448
{% endtab %}
449+
450+
{% tab title="Python" %}
451+
```python
452+
# Build the transaction
453+
transaction = (
454+
TokenCreateTransaction()
455+
.set_token_name("Demo Token")
456+
.set_token_symbol("DEMO")
457+
.set_decimals(2)
458+
.set_initial_supply(100_000)
459+
.set_token_type(TokenType.FUNGIBLE_COMMON)
460+
.set_supply_type(SupplyType.FINITE)
461+
.set_max_supply(100_000)
462+
.set_treasury_account_id(operatorId)
463+
.freeze_with(client)
464+
)
465+
466+
# Sign with the admin key and execute
467+
signed_tx = transaction.sign(admin_key)
468+
receipt = signed_tx.execute(client)
469+
token_id = receipt.token_id
470+
471+
print(f"\nFungible token created: {token_id}")
472+
```
473+
{% endtab %}
350474
{% endtabs %}
351475

352476
## Step 4: Query the Treasury Balance Using Mirror Node API
@@ -394,6 +518,14 @@ mirrorNodeUrl := "https://testnet.mirrornode.hedera.com/api/v1/accounts/" + oper
394518
```
395519
{% endcode %}
396520
{% endtab %}
521+
522+
{% tab title="Python" %}
523+
{% code overflow="wrap" %}
524+
```python
525+
mirror_node_url = f"https://testnet.mirrornode.hedera.com/api/v1/accounts/{operatorId}/tokens?token.id={token_id}"
526+
```
527+
{% endcode %}
528+
{% endtab %}
397529
{% endtabs %}
398530

399531
**Complete Implementation:**
@@ -483,6 +615,29 @@ if len(data.Tokens) > 0 {
483615
client.Close()
484616
```
485617
{% endtab %}
618+
{% tab title="Python" %}
619+
```python
620+
# Wait for Mirror Node to populate data
621+
print("\nWaiting for Mirror Node to update...")
622+
time.sleep(3)
623+
624+
# Query balance using Mirror Node
625+
mirror_node_url = f"https://testnet.mirrornode.hedera.com/api/v1/accounts/{operatorId}/tokens?token.id={token_id}"
626+
627+
response = requests.get(mirror_node_url, timeout=10)
628+
response.raise_for_status()
629+
data = response.json()
630+
631+
tokens = data.get("tokens", [])
632+
if tokens:
633+
balance = tokens[0].get("balance", 0)
634+
print(f"\nTreasury holds: {balance} DEMO\n")
635+
else:
636+
print("Token balance not yet available in Mirror Node")
637+
638+
client.close()
639+
```
640+
{% endtab %}
486641
{% endtabs %}
487642

488643
***
@@ -732,6 +887,81 @@ func main() {
732887

733888
</details>
734889

890+
<details>
891+
892+
<summary><strong>Python</strong></summary>
893+
894+
{% code overflow="wrap" %}
895+
```python
896+
import os
897+
import time
898+
import requests
899+
900+
from hiero_sdk_python import (
901+
Client,
902+
AccountId,
903+
PrivateKey,
904+
TokenCreateTransaction,
905+
TokenType,
906+
SupplyType,
907+
)
908+
909+
# Load your operator credentials
910+
operatorId = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
911+
operatorKey = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
912+
913+
# Initialize the client for testnet
914+
client = Client()
915+
client.set_operator(operatorId, operatorKey)
916+
917+
# Generate token keys
918+
supply_key = PrivateKey.generate_ecdsa()
919+
admin_key = supply_key
920+
921+
# Build & execute the token creation transaction
922+
transaction = (
923+
TokenCreateTransaction()
924+
.set_token_name("Demo Token")
925+
.set_token_symbol("DEMO")
926+
.set_decimals(2)
927+
.set_initial_supply(100_000)
928+
.set_token_type(TokenType.FUNGIBLE_COMMON)
929+
.set_supply_type(SupplyType.FINITE)
930+
.set_max_supply(100_000)
931+
.set_treasury_account_id(operatorId)
932+
.freeze_with(client)
933+
)
934+
935+
signed_tx = transaction.sign(admin_key)
936+
receipt = signed_tx.execute(client)
937+
token_id = receipt.token_id
938+
939+
print(f"\nFungible token created: {token_id}")
940+
941+
# Wait for Mirror Node to populate data
942+
print("\nWaiting for Mirror Node to update...")
943+
time.sleep(3)
944+
945+
# Query balance using Mirror Node
946+
mirror_node_url = f"https://testnet.mirrornode.hedera.com/api/v1/accounts/{operatorId}/tokens?token.id={token_id}"
947+
948+
response = requests.get(mirror_node_url, timeout=10)
949+
response.raise_for_status()
950+
data = response.json()
951+
952+
tokens = data.get("tokens", [])
953+
if tokens:
954+
balance = tokens[0].get("balance", 0)
955+
print(f"\nTreasury holds: {balance} DEMO\n")
956+
else:
957+
print("Token balance not yet available in Mirror Node")
958+
959+
client.close()
960+
```
961+
{% endcode %}
962+
963+
</details>
964+
735965
***
736966

737967
## Run Your Project
@@ -766,6 +996,19 @@ mvn compile exec:java -Dexec.mainClass="com.example.CreateTokenDemo"
766996
```bash
767997
go run create_token_demo.go
768998
```
999+
{% endtab %}
1000+
1001+
{% tab title="Python" %}
1002+
```bash
1003+
python CreateTokenDemo.py
1004+
```
1005+
1006+
**When finished, deactivate the virtual environment:**
1007+
1008+
```bash
1009+
deactivate
1010+
```
1011+
7691012
{% endtab %}
7701013
{% endtabs %}
7711014

0 commit comments

Comments
 (0)