This commit is contained in:
Xavier Fiechter 2024-02-09 22:47:46 +01:00
parent 8b2145e653
commit 06b3e6f6c2
2 changed files with 237 additions and 0 deletions

View file

@ -0,0 +1,143 @@
# Integrating the Labelbase API: A Step-by-Step Python Guide
Welcome to the Labelbase API integration Python tutorial! In this guide, we'll walk you through the process of seamlessly integrating the Labelbase API into your project. The Labelbase API allows you to manage labelbases and labels, making it an invaluable tool for organizing and syncing your BIP-329 Bitcoin labels and coin information.
Below is a step-by-step guide that walks you through the process of using the Labelbase API to manage labelbases and labels. I've included explanations, code snippets, and comments to assist you in understanding each step.
Please note that you need to replace 'your_key_here' with [your actual API key](https://labelbase.space/account/apikey/).
```
# Import required libraries
import requests # pip install requests
import json
# Replace 'your_key_here' with your actual API key
api_key = 'your_key_here'
# Define the base URL for the Labelbase API
base_url = 'https://labelbase.space/api/v0/'
# Define headers containing the API key
headers = {
'Authorization': f'Token {api_key}' # Authentication scheme is 'Token' not 'Bearer' or 'JWT'
}
```
```
# Create a new Labelbase
url = f'{base_url}labelbase/'
data = {
'name': 'Bitcoin Chronicles',
'fingerprint': 'ticktock',
'about': 'Legendary transactions and important addresses.'
}
response = requests.post(url, headers=headers, data=data)
labelbase_id = json.loads(response.text).get('id')
print(f'Created Labelbase with ID: {labelbase_id}')
```
**First Bitcoin Transaction:** On January 12, 2009, the first known Bitcoin transaction took place between Satoshi Nakamoto (the pseudonymous creator of Bitcoin) and Hal Finney. Nakamoto sent 10 BTC to Finney as a test.
```
# Create two labels within the created Labelbase
url = f'{base_url}labelbase/{labelbase_id}/label/'
data = {
'type': 'tx',
'ref': 'F4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
'label': 'Satoshi sending sats'
}
response = requests.post(url, headers=headers, data=data)
print(json.loads(response.text))
label_id = json.loads(response.text).get('id')
print(f'Created Label with ID: {label_id}')
```
**Pizza Purchase:** On May 22, 2010, a programmer named Laszlo Hanyecz famously paid 10,000 BTC (worth millions of dollars today) for two pizzas, making it one of the earliest examples of a real-world transaction using Bitcoin.
```
data = {
'type': 'tx',
'ref': 'Cca7507897abc89628f450e8b1e0c6fca4ec3f7b34cccf55f3f531c659ff4d79',
'label': 'Two supreme pizzas from Papa John\'s'
}
response = requests.post(url, headers=headers, data=data)
print(json.loads(response.text))
label_id = json.loads(response.text).get('id')
print(f'Created Label with ID: {label_id}')
```
```
# List all labels within a Labelbase
url = f'{base_url}labelbase/{labelbase_id}/label/'
response = requests.get(url, headers=headers)
labels = json.loads(response.text)
print('Labels within the Labelbase with ID {labelbase_id}:')
for label in labels:
print(label)
```
To perform label updates, you have two options.
You can either provide the label ID along with the updated data, or you can retrieve the existing label data using a known ID, modify the relevant JSON fields, and then send the modified data. Keep in mind that in the latter approach, you should replace 'your_satoshi_label_id' with the specific label ID you are working with.
```
# Update a label within a Labelbase
# V1
data = {
'type': 'tx',
'ref': 'F4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
'label': 'First transaction from Satoshi to Hal Finney'
}
label_id = 'your_satoshi_label_id'
url = f'{base_url}labelbase/{labelbase_id}/label/{label_id}/'
response = requests.put(url, headers=headers, data=data)
print(json.loads(response.text))
label_id = json.loads(response.text).get('id')
print(f'V1) Updated Label with ID: {label_id}')
```
```
# V2
for label in labels:
if label.get('id') == 'your_satoshi_label_id':
data = label
assert data
data['label'] = 'First transaction from Satoshi to Hal Finney'
label_id = data.get('id')
url = f'{base_url}labelbase/{labelbase_id}/label/{label_id}/'
response = requests.put(url, headers=headers, data=data)
print(json.loads(response.text))
label_id = json.loads(response.text).get('id')
print(f'V2) Updated Label with ID: {label_id}')
```
```
# Delete a label within a Labelbase
url = f'{base_url}labelbase/{labelbase_id}/label/{label_id}/'
response = requests.delete(url, headers=headers, data={})
print(f'Deleted Label with ID: {label_id}')
```

94
_Examples/api.md Normal file
View file

@ -0,0 +1,94 @@
#
https://labelbase.space/api/labelbase/
import requests # pip install requests
import json
# Replace 'your_key_here' with your actual API key
api_key = 'your_key_here'
# Replace with the ID of your Labelbase
labelbase_id = 17
# URL of the Labelbase API endpoint
url = f'https://labelbase.space/api/v0/labelbase/{labelbase_id}/'
# Data to be sent in the POST request (if needed)
data = {
}
# Headers containing the API key
headers = {
'Authorization': f'Token {api_key}'
}
# Make the POST request
response = requests.post(url, headers=headers, data=data)
# Print the response
print(response.status_code)
print(response.text)
>>> import json
>>> json.loads(response.text)
{'id': 30, 'name': '', 'fingerprint': '', 'about': ''}
# List all labelbases
url = f'https://labelbase.space/api/v0/labelbase/'
response = requests.get(url, headers=headers, data=data)
# Create a new Labelbase and store its ID for further usage.
url = f'https://labelbase.space/api/v0/labelbase/'
data = {'name': 'Bitcoin Chronicles',
'fingerprint': 'ticktock', 'about': 'Legendary transactions and important addresses.'}
response = requests.post(url, headers=headers, data=data)
labelbase_id = json.loads(response.text).get('id')
# Create a new label for a transaction
url = f'https://labelbase.space/api/v0/labelbase/{labelbase_id}/label/'
txid = "Cca7507897abc89628f450e8b1e0c6fca4ec3f7b34cccf55f3f531c659ff4d79"
f"/api/v0/labelbase/{labelbase_id}/label/"
# Sending no data / data={} creates an empty label
response = requests.post(url, headers=headers, data=data)
#
data = {
"labelbase": labelbase_id,
"type": "tx",
"ref": txid,
"label": "Two supreme pizzas from Papa John's"
}
response = requests.post(url, headers=headers, data=data)
# Create and update a label
data = {
"labelbase": labelbase_id,
"type": "tx",
"ref": "F4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16",
"label": "Satoshi sending sats"
}
response = requests.post(url, headers=headers, data=data)
label_data = json.loads(response.text)
label_id = label_data.get('id')
url = f'https://labelbase.space/api/v0/labelbase/{labelbase_id}/label/{label_id}/'
data["label"] = "First transaction from Satoshi to Hal Finney"
response = requests.put(url, headers=headers, data=data)
# List all labels of a labelbase
url = f'https://labelbase.space/api/v0/labelbase/{labelbase_id}/label/'
response = requests.get(url, headers=headers, data={})
response_data = json.loads(response.text)
for l in response_data:
print(l)