Server Quickstart (Kotlin)¶
This guide will help you build a payment-protected REST API server using Kotlin and OpenLibx402 with coroutines.
Prerequisites¶
- Kotlin 1.9+ with JDK 11 or higher
- Gradle 7+ or Maven 3.6+
- Basic understanding of Kotlin coroutines
- Solana wallet with some SOL (for transaction fees)
Overview¶
Build modern, asynchronous payment-protected API servers with Kotlin's coroutines and popular frameworks like Ktor or Spring Boot. This guide shows you how to integrate X402 payment verification with idiomatic Kotlin code.
Project Setup¶
Using Gradle (Kotlin DSL)¶
Ktor Server Implementation¶
1. Payment Models¶
2. Payment Verification Service¶
3. X402 Plugin¶
4. Routing with Payment Protection¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
5. Main Application¶
6. Application Configuration¶
Running the Server¶
The server will start on http://localhost:8080.
Testing the Server¶
Test Free Endpoint¶
Response:
Test Premium Endpoint (No Payment)¶
Response:
Test Premium Endpoint (With Payment)¶
Response:
Spring Boot Alternative¶
Build Configuration¶
Controller Example¶
Best Practices¶
1. Use Coroutines for Async Operations¶
2. Implement Payment Caching¶
3. Handle Errors Gracefully¶
Next Steps¶
- Learn about client usage: Client Quickstart
- Explore API reference: API Reference
- Understand error handling: Error Reference
- See complete examples: Examples
See Also¶
- Java Server Quickstart - Java implementation
- Go Server Quickstart - Go implementation with middleware
- Python FastAPI Guide - Python server implementation
- TypeScript Express Guide - TypeScript server implementation