This directory contains examples demonstrating how to use the Authzed Java client library from various JVM languages.
CallingCheck.java- Demonstrates basic permission checking with schema writing, relationship creation, and permission checksCallingWatch.java- Demonstrates watching for changes in SpiceDB with automatic reconnection
CallingCheck.kt- Same functionality as the Java example, but using idiomatic Kotlin syntax
CallingCheck.scala- Same functionality as the Java example, but using idiomatic Scala syntax
The Authzed Java client library works seamlessly with all JVM languages. Choose your build tool and language:
Note: You need to include a gRPC transport implementation like grpc-netty-shaded to run these examples. The authzed library doesn't include a transport by default to give you flexibility in choosing one.
<dependencies>
<dependency>
<groupId>com.authzed.api</groupId>
<artifactId>authzed</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-api</artifactId>
<version>1.75.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.75.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.75.0</version>
</dependency>
</dependencies>For build.gradle:
dependencies {
implementation "com.authzed.api:authzed:1.5.4"
implementation 'io.grpc:grpc-api:1.75.0'
implementation 'io.grpc:grpc-stub:1.75.0'
implementation 'io.grpc:grpc-netty-shaded:1.75.0'
}For build.gradle.kts (Kotlin DSL):
dependencies {
implementation("com.authzed.api:authzed:1.5.4")
implementation("io.grpc:grpc-api:1.75.0")
implementation("io.grpc:grpc-stub:1.75.0")
implementation("io.grpc:grpc-netty-shaded:1.75.0")
}libraryDependencies ++= Seq(
"com.authzed.api" % "authzed" % "1.5.4",
"io.grpc" % "grpc-api" % "1.75.0",
"io.grpc" % "grpc-stub" % "1.75.0",
"io.grpc" % "grpc-netty-shaded" % "1.75.0"
)All examples follow the same pattern:
- Create a channel - Connect to SpiceDB using gRPC
- Authenticate - Use a bearer token for authentication
- Create service stubs - Initialize the schema and permissions services
- Write a schema - Define your permission model
- Write relationships - Create relationships between objects
- Check permissions - Verify if a subject has permission on a resource
- Uses verbose builder pattern
- Requires explicit exception handling with try-catch
- Traditional class structure
- More concise syntax with named parameters and string interpolation
- Better null safety with nullable types (
?) - Can use
try-catchas expressions - Cleaner lambda syntax
- Functional programming style with
TryandOptionmonads - Pattern matching with
recover - For-comprehensions for sequential operations (not shown in basic example)
- Immutable by default
These examples are meant to be copied into your own project. They demonstrate the API usage patterns but are not standalone runnable programs without:
- A valid SpiceDB instance (local or cloud)
- A valid authentication token
- Proper build configuration for your language
For a complete guide on integrating SpiceDB with your application, see the Protecting Your First App guide.