---
updatedAt: 2025-11-19T20:08:45.000Z
agentTools:
  projectIndex: https://docs.uplynk.com/llms.txt
---

# API Auth for Classic Authentication (V2 APIs)

```python Python
import base64, hashlib, hmac, json, time, urllib.parse, zlib
from urllib.request import urlopen

ROOT_URL = 'https://services.uplynk.com'
USER_ID = '1234567890abcdefghijklmnopqrstu' # Replace with your user ID.
API_KEY = '1234567890abcdefghijklmnopqrstuvwxyz1234' # Replace with your API key.

def Call(uri, **msg):
    """Base method for Uplynk API (api2) calls"""
    msg['_owner'] = USER_ID
    msg['_timestamp'] = int(time.time())
    msg = json.dumps(msg)
    msg = zlib.compress(msg.encode(), level=9)
    msg = base64.b64encode(msg).strip()
    sig = hmac.new(API_KEY.encode(), msg, hashlib.sha256).hexdigest()
    body = urllib.parse.urlencode(dict(msg=msg, sig=sig)).encode('utf-8')
    return json.loads(urlopen(ROOT_URL + uri, body).read())
```

# Imports

<!-- python@1-2 -->

These modules are used for:  
	•	Data handling: json, time, zlib  
	•	Security: hmac, hashlib, base64  
	•	HTTP requests: urllib.request.urlopen, urllib.parse

# Configuration

<!-- python@4-6 -->

Set your base URL and authentication credentials:  
	•	USER_ID: Your unique account identifier  
	•	API_KEY: Your API key for signing requests

These should be stored securely in production (e.g., environment variables).

# Define the Call Function

<!-- python@8 -->

•	uri: Endpoint path to call (e.g., /api2/job/list)  
	•	\*\*msg: Arbitrary keyword arguments that form the body of your API request

# Add Authentication Fields

<!-- python@10-11 -->

These fields are required by Uplynk’s API:  
	•	\_owner: Identifies the user making the request  
	•	\_timestamp: Current Unix timestamp to prevent replay attacks

# Serialize and Compress the Message

<!-- python@12-13 -->

•	Convert the message dictionary to a JSON string  
	•	Compress it using zlib for smaller payload size

# Base64 Encode the Message

<!-- python@14 -->

The compressed message is encoded in base64 so it can be safely sent as a URL parameter.

# Generate HMAC Signature

<!-- python@15 -->

•	Sign the base64 message with your API key using HMAC-SHA256  
	•	Produces a secure signature to authenticate the request

# Build the Request Body

<!-- python@16 -->

•	Package the encoded message and signature as form data  
	•	Prepare it for transmission in a POST request

# Send the Request and Parse Response

<!-- python@17 -->

•	Make a POST request to the full URL  
	•	Read and parse the JSON response into a Python dictionary