Tetron Gitbook
X/TwitterWebsite
  • Tetron.fun - Introduction
  • Tetron.fun - Platform Overview
  • Tetron.fun - Getting Started
  • Tetron.fun - Private RPC Nodes
  • Tetron.fun - Troubleshooting
  • Tetron.fun - API Refference
  • Tetron.fun - Webhooks & Event Subscriptions
  • Tetron.fun - Code Examples & Tutorials
  • Tetron.fun - Support
Powered by GitBook
On this page
  • Code Examples & Tutorials
  • 1. Building a Payment Approval Microservice

Tetron.fun - Code Examples & Tutorials

Step‑by‑step integration scenarios and sample applications.

Code Examples & Tutorials

This section provides guided walkthroughs for common use cases, complete with sample code, configuration tips, and best‑practice advice.

1. Building a Payment Approval Microservice

Overview

Create a Node.js microservice that automatically approves or rejects payments based on fraud and reputation scores.

Steps

  1. Initialize project and install @tetronfun/sdk

  2. Configure environment variables for API credentials

  3. Implement /submit endpoint to forward transaction to TetronFun

  4. Handle response and send approval callback

Sample Code (Node.js)

import express from 'express';
import TetronFun from '@tetronfun/sdk';

const app = express();
app.use(express.json());

const client = new TetronFun({ apiKey: process.env.KEY, apiSecret: process.env.SECRET });

app.post('/submit', async (req, res) => {
  const { from, to, amount, currency, network } = req.body;
  const result = await client.score({ from_address: from, to_address: to, amount, currency, network });
  const action = result.recommendation === 'approve' ? 'approved' : 'rejected';
  res.json({ status: action, details: result });
});

app.listen(3000);
2. Integrating with a React Frontend

Overview
Embed real‑time score updates in a React dashboard using WebSocket subscriptions.

Steps
Install ws or browser WebSocket
Authenticate and subscribe to scores channel
Display live updates in a component
Sample Code (React)
import { useEffect, useState } from 'react';

export default function LiveScores() {
  const [scores, setScores] = useState([]);

  useEffect(() => {
    const ws = new WebSocket('wss://api.tetronfun.com/v1/stream');
    ws.onopen = () => {
      ws.send(JSON.stringify({ action: 'authenticate', apiKey: process.env.REACT_APP_KEY }));
      ws.send(JSON.stringify({ action: 'subscribe', channels: ['scores'] }));
    };
    ws.onmessage = evt => {
      const msg = JSON.parse(evt.data);
      setScores(prev => [msg.data, ...prev].slice(0, 10));
    };
    return () => ws.close();
  }, []);

  return (
    <ul>
      {scores.map(s => (
        <li key={s.transaction_id}>
          {s.transaction_id}: Fraud {s.fraud_score}, Rep {s.reputation_score}
        </li>
      ))}
    </ul>
  );
}
PreviousTetron.fun - Webhooks & Event SubscriptionsNextTetron.fun - Support

Last updated 12 days ago