====== API Cookbook — Automation Examples ======
===== Poll for New Records (Python) =====
Check for new records every 5 minutes and process them:
import time, requests, json
API_URL = 'https://wherewelearn.com/api/apic.php'
AUTH = {'u':'APIUSER','k':'APIKEY','r':'COLLREF','g':'8'}
STATE_FILE = 'last_processed_id.txt'
def get_last_id():
try: return int(open(STATE_FILE).read().strip())
except: return 0
def save_last_id(n):
open(STATE_FILE,'w').write(str(n))
def process(record):
print(f"Processing: {record['strD5_1_1']}")
# your logic here
while True:
last_id = get_last_id()
r = requests.post(API_URL, data={**AUTH, 'f':'crosslist', 'per_page':'100'}, verify=False)
records = r.json().get('data',{}).get('records',[])
new_records = [rec for rec in records if int(rec['intD5_Id']) > last_id]
for rec in sorted(new_records, key=lambda x: int(x['intD5_Id'])):
process(rec)
save_last_id(int(rec['intD5_Id']))
time.sleep(300) # 5 minutes
===== Sync Collection Records to a Google Sheet =====
import requests, gspread
from oauth2client.service_account import ServiceAccountCredentials
API_URL = 'https://wherewelearn.com/api/apic.php'
AUTH = {'u':'APIUSER','k':'APIKEY','r':'COLLREF','g':'7'}
# Get all records from LEAST
r = requests.post(API_URL, data={**AUTH,'f':'crosslist','per_page':'1000'}, verify=False)
records = r.json()['data']['records']
# Write to Google Sheet
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scope)
client = gspread.authorize(creds)
sheet = client.open('LEAST Export').sheet1
headers = list(records[0].keys()) if records else []
sheet.clear()
sheet.append_row(headers)
for rec in records:
sheet.append_row(list(rec.values()))
print(f"Exported {len(records)} records to Google Sheets")
===== Create a Record from a Web Form =====
'Title required']); exit; }
$ch = curl_init('https://wherewelearn.com/api/apic.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => http_build_query([
'u' => 'APIUSER', 'k' => 'APIKEY',
'r' => 'COLLREF', 'g' => 'WRITEVIEWID', 'f' => 'create',
'strD5_1_1' => $title,
'strD5_1_2' => $description,
'notes' => 'Submitted via web form from ' . $_SERVER['REMOTE_ADDR'],
]),
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
echo json_encode(['success' => $result['data']['fnerr'] === 0,
'id' => $result['data']['dxid'] ?? null]);