> For the complete documentation index, see [llms.txt](https://tetron.gitbook.io/tetron/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tetron.gitbook.io/tetron/tetron.fun-code-examples-and-tutorials.md).

# Tetron.fun - Code Examples & Tutorials

## 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)

```javascript
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>
  );
}
```
