# Architecture

## System Overview

<figure><img src="/files/P1CAGISp6r3Pgka40iVV" alt=""><figcaption></figcaption></figure>

#### Payment Flow Explained

**Step 1: x402 Request**

* Client makes HTTP request to protected API endpoint
* x402 middleware checks for payment proof
* If missing, server returns 402 with payment instructions

**Step 2: ZK Proof Generation (Client-Side)**

* Client generates zero-knowledge proof (5-10 seconds)
* Proves sender has sufficient funds without revealing amount
* Encrypts payment amount using ElGamal encryption
* Creates proof that encrypted value is within valid range

**Step 3: Proof Verification**

* NumPay verifier validates ZK proof cryptographically
* Checks proof structure and public signals
* Validates phone/wallet identity mapping
* Returns JWT payment token if valid

**Step 4: Payment Settlement**

* Client submits payment token to settlement endpoint
* NumPay relayer decrypts amount (server-side only)
* Creates and signs blockchain transaction
* Executes transfer via smart contract (gasless for user)

**Step 5: On-Chain Confirmation**

* Transaction confirmed on blockchain
* If using escrow: balance updated atomically
* If direct: wallet-to-wallet transfer complete
* Phone identity mapping preserved with encryption

**Step 6: Resource Delivery**

* API server receives settlement notification
* Validates payment token authenticity
* Grants access to requested resource
* Returns 200 OK with content


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://numpay.gitbook.io/numpay/technology-architecture/numpay-shadow-protocol-zk-+-x402/architecture.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
