Mogami
  • Home
  • Roadmap & ideas
  • x402 in French
  • Java client SDK
    • Getting started
    • Example client
  • Java server SDK
    • Getting started
    • Example server
    • Configuration
  • Faciliator server
    • Getting started
    • Verifications
  • Learning X402
    • Protocol explanation
      • Get /weather without X-PAYMENT
      • Get /weather with X-PAYMENT
      • Payment verification on server
      • Payment settlement on server
    • X402 hands-on
    • Useful links
  • Contact
    • Email
    • Twitter
Powered by GitBook
On this page
  • Running our example server
  • Running a client
  1. Java server SDK

Example server

PreviousGetting startedNextConfiguration

Last updated 19 days ago

Running our example server

If you’d like a working example, you can obtain the example server source code by clicking or by running git clone https://github.com/mogami-tech/x402-examples.git.

Once retrieved, go inside the project directory and type:

mvn spring-boot:run

The server will start on the 4021 port, you can check with this command:

curl localhost:4021/weather

You will get this kind of result:

{
  "x402Version": 1,
  "accepts": [
    {
      "scheme": "exact",
      "network": "base-sepolia",
      "maxAmountRequired": "1000",
      "resource": "http://localhost:4021/weather",
      "description": "",
      "mimeType": "",
      "payTo": "0x7553F6FA4Fb62986b64f79aEFa1fB93ea64A22b1",
      "maxTimeoutSeconds": 60,
      "asset": "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
      "extra": {
        "name": "USDC",
        "version": "2"
      }
    }
  ],
  "error": "Payment required"
}

Running a client

For the moment, we will use the official Coinbase client to call our example service with a payment. Get it by typing:

git clone git@github.com:coinbase/x402.git
cd x402/examples/typescript/clients/fetch
cp .env-local .env

Edit the .env and set this:

RESOURCE_SERVER_URL=http://localhost:4021
ENDPOINT_PATH=/weather
ADDRESS=0xAbb25843e9cba098361735eBC79a7fc47019b117
PRIVATE_KEY=0x9b9569df59719e5a3e6a82eb180a5435bec9a4aafb657e351be9ce47ed6cfcb7

Run the client with:

pnpm install
pnpm dev

You will get a message like this:

{ report: { weather: 'sunny', temperature: 70 } }
{
  success: true,
  transaction: '0x83645ca1dd0a5a59653748c8a63dd65126a8dd6097ca3f98fe4b27325f2fa92f',
  network: 'base-sepolia',
  payer: '0x2980bc24bBFB34DE1BBC91479Cb712ffbCE02F73'
}

The transaction can be seen .

here
here