Skip to main content

Python Example

Use this when you want to send the vendor-number callback from Python. Instantiate with base_url and API key (e.g. from env), then call assign_vendor_number(event_id, vendor_number). Returns True on 204.

import hmac
import hashlib
import base64
import json
import requests
from typing import Optional

class B1LinkWebhookClient:
def __init__(self, api_key: str, base_url: str):
self.api_key = api_key
self.base_url = base_url

def assign_vendor_number(self, event_id: str, vendor_number: str) -> bool:
"""
Assign vendor number to an onboarding request ERP event.

Args:
event_id: The onboarding request ERP event ID (GUID)
vendor_number: The vendor number assigned by the ERP system

Returns:
True if successful, False otherwise
"""
# Create request payload
payload = {
"Id": event_id,
"ErpVendorNumber": vendor_number
}

# Serialize to JSON (compact format, no whitespace)
json_payload = json.dumps(payload, separators=(',', ':'))

# Generate signature
signature = self._generate_signature(json_payload)

# Create request URL
url = f"{self.base_url}/api/onboarding-request-erp-events/assign-erp-vendor-number-event"

# Set headers
headers = {
"Content-Type": "application/json",
"X-B1LINK-Signature": signature
}

# Send request
response = requests.put(url, json=payload, headers=headers)
return response.status_code == 204

def _generate_signature(self, payload: str) -> str:
"""
Generate HMAC-SHA256 signature for the payload.

Args:
payload: JSON string of the request body

Returns:
Base64-encoded signature
"""
# Convert to bytes
payload_bytes = payload.encode('utf-8')
key_bytes = self.api_key.encode('utf-8')

# Compute HMAC-SHA256
hmac_hash = hmac.new(key_bytes, payload_bytes, hashlib.sha256)

# Base64 encode
return base64.b64encode(hmac_hash.digest()).decode('utf-8')

# Usage
client = B1LinkWebhookClient(
api_key="your-api-key-here",
base_url="{baseUrl}"
)

success = client.assign_vendor_number(
event_id="550e8400-e29b-41d4-a716-446655440000",
vendor_number="VENDOR-12345"
)

Key Points

  • Uses json.dumps() with separators=(',', ':') for compact JSON
  • hmac.new() with hashlib.sha256 for signature generation
  • base64.b64encode() for Base64 encoding
  • requests library for HTTP requests

Dependencies

Install required package:

pip install requests

Error Handling

You may want to add error handling:

try:
success = client.assign_vendor_number(event_id, vendor_number)
if success:
print("Vendor number assigned successfully")
else:
print("Failed to assign vendor number")
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")

Next step