Skip to main content

Integration Examples

Examples of integrating PingPanda into various platforms and languages.

Node.js / JavaScript

async function sendEvent(category, fields, description) {
const response = await fetch('https://pingpanda.com/api/v1/events', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.PINGPANDA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
category,
fields,
description,
}),
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.message);
}

return await response.json();
}

// Usage
await sendEvent('sale', {
amount: 49.99,
customer: 'john@example.com',
}, 'New sale completed!');

Python

import requests
import os

def send_event(category, fields=None, description=None):
url = 'https://pingpanda.com/api/v1/events'
headers = {
'Authorization': f'Bearer {os.getenv("PINGPANDA_API_KEY")}',
'Content-Type': 'application/json',
}
data = {
'category': category,
}
if fields:
data['fields'] = fields
if description:
data['description'] = description

response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
return response.json()

# Usage
send_event('sale', {
'amount': 49.99,
'customer': 'john@example.com',
}, 'New sale completed!')

PHP

function sendEvent($category, $fields = null, $description = null) {
$url = 'https://pingpanda.com/api/v1/events';
$apiKey = getenv('PINGPANDA_API_KEY');

$data = ['category' => $category];
if ($fields) {
$data['fields'] = $fields;
}
if ($description) {
$data['description'] = $description;
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
]);

$response = curl_exec($ch);
curl_close($ch);

return json_decode($response, true);
}

// Usage
sendEvent('sale', [
'amount' => 49.99,
'customer' => 'john@example.com',
], 'New sale completed!');

Go

package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)

func sendEvent(category string, fields map[string]interface{}, description string) error {
url := "https://pingpanda.com/api/v1/events"
apiKey := os.Getenv("PINGPANDA_API_KEY")

data := map[string]interface{}{
"category": category,
}
if fields != nil {
data["fields"] = fields
}
if description != "" {
data["description"] = description
}

jsonData, _ := json.Marshal(data)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API error: %d", resp.StatusCode)
}

return nil
}

// Usage
sendEvent("sale", map[string]interface{}{
"amount": 49.99,
"customer": "john@example.com",
}, "New sale completed!")

Webhook Integration (Stripe Example)

// Stripe webhook handler
app.post('/webhooks/stripe', async (req, res) => {
const event = req.body;

if (event.type === 'checkout.session.completed') {
await fetch('https://pingpanda.com/api/v1/events', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.PINGPANDA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
category: 'sale',
fields: {
amount: event.data.object.amount_total / 100,
customer: event.data.object.customer_email,
},
description: 'New payment received!',
}),
});
}

res.json({ received: true });
});