python
import requests
api_key = "YourKey"
# Set your API key and headers
headers = {
"accept": "application/json",
"X-API-Key": api_key,
"content-type": "application/json",
}
# Step 1: Create the stream
create_stream_url = "https://api.moralis-streams.com/streams/evm"
create_stream_body = {
"webhookUrl": "https://webhook.site/9449a6d0-ebcc-4e8f-97aa-adcb9abb6a7c",
"description": "1234",
"tag": "1234",
"includeNativeTxs": True,
"chainIds": ["0x1"],
}
# Send the PUT request to create the stream
create_response = requests.put(
create_stream_url, headers=headers, json=create_stream_body
)
create_data = create_response.json()
# Check if the creation was successful and extract the stream ID
if create_response.status_code == 201:
stream_id = create_data.get("id")
print(f"Created Stream ID: {stream_id}")
else:
print("Failed to create the stream:", create_data)
exit()
# Step 2: Update the stream using the captured stream ID
update_stream_url = f"https://api.moralis-streams.com/streams/evm/{stream_id}"
update_body = {
"webhookUrl": "https://webhook.site/9449a6d0-ebcc-4e8f-97aa-adcb9abb6a7c",
"description": "1234update",
"tag": "134update",
"includeNativeTxs": True,
"includeContractLogs": True,
"topic0": ["Transfer(address,address,uint256)"],
"abi": [
{
"anonymous": False,
"inputs": [
{"indexed": True, "name": "from", "type": "address"},
{"indexed": True, "name": "to", "type": "address"},
{"indexed": True, "name": "value", "type": "uint256"},
],
"name": "Transfer",
"type": "event",
}
],
}
# Send the POST request to update the stream
update_response = requests.post(update_stream_url, headers=headers, json=update_body)
# Print the update response
print(update_response.status_code)
print(update_response.json())
javascript
const apiKey = "YourKey";
const headers = {
"accept": "application/json",
"X-API-Key": apiKey,
"content-type": "application/json"
};
const createStreamUrl = "https://api.moralis-streams.com/streams/evm";
const createStreamBody = {
"webhookUrl": "https://webhook.site/9449a6d0-ebcc-4e8f-97aa-adcb9abb6a7c",
"description": "1234",
"tag": "1234",
"includeNativeTxs": true,
"chainIds": ["0x1"]
};
async function createStream() {
try {
const createResponse = await fetch(createStreamUrl, {
method: "PUT",
headers: headers,
body: JSON.stringify(createStreamBody)
});
const createData = await createResponse.json();
if (createResponse.status === 201) {
const streamId = createData.id;
console.log(`Created Stream ID: ${streamId}`);
await updateStream(streamId);
} else {
console.log("Failed to create the stream:", createData);
}
} catch (error) {
console.error("Error creating the stream:", error);
}
}
async function updateStream(streamId) {
const updateStreamUrl = `https://api.moralis-streams.com/streams/evm/${streamId}`;
const updateBody = {
"webhookUrl": "https://webhook.site/9449a6d0-ebcc-4e8f-97aa-adcb9abb6a7c",
"description": "1234update",
"tag": "134update",
"includeNativeTxs": true,
"includeContractLogs": true,
"topic0": ["Transfer(address,address,uint256)"],
"abi": [
{
"anonymous": false,
"inputs": [
{ "indexed": true, "name": "from", "type": "address" },
{ "indexed": true, "name": "to", "type": "address" },
{ "indexed": true, "name": "value", "type": "uint256" }
],
"name": "Transfer",
"type": "event"
}
]
};
try {
const updateResponse = await fetch(updateStreamUrl, {
method: "POST",
headers: headers,
body: JSON.stringify(updateBody)
});
const updateData = await updateResponse.json();
console.log("Update Response Status:", updateResponse.status);
console.log("Update Response Data:", updateData);
} catch (error) {
console.error("Error updating the stream:", error);
}
}
createStream();