> ## Documentation Index
> Fetch the complete documentation index at: https://docs.twexapi.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Java SDK

> Use the TwexAPI Java SDK for tweet search, follower exports, DMs, and X automation in Spring and JVM backends.

Use the Java SDK when you want typed builders, CompletableFuture support, retries, and pagination helpers for TwexAPI REST workflows in Spring Boot, worker, and JVM backend applications.

| Java task      | SDK call                         | Save         |
| -------------- | -------------------------------- | ------------ |
| Search tweets  | `sdk.search().advanced()`        | `nextCursor` |
| Read profile   | `sdk.users().getAbout()`         | `screenName` |
| List followers | `sdk.users().followers().list()` | cursor       |

## Install

Gradle:

```groovy theme={null}
implementation 'io.twexapi:x-api-scraper:0.1.0'
```

Maven:

```xml theme={null}
<dependency>
    <groupId>io.twexapi</groupId>
    <artifactId>x-api-scraper</artifactId>
    <version>0.1.0</version>
</dependency>
```

Maven Central publication is pending. Build from source at [twexapi-dev/x-api-scraper-java](https://github.com/twexapi-dev/x-api-scraper-java).

## Authenticate

```bash theme={null}
export X_API_SCRAPER_KEY="YOUR_API_KEY"
```

```java theme={null}
import io.twexapi.sdk.XApiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;

XApiScraper sdk = XApiScraper.builder()
        .bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
        .build();

AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
        .searchTerms(java.util.List.of("from:elonmusk"))
        .sortBy("Latest")
        .nextCursor("")
        .build();

var res = sdk.search().advanced().request(req).call();
```

## Basic example

Look up a profile and list followers:

```java theme={null}
var about = sdk.users().getAbout()
        .screenName("elonmusk")
        .call();

var followers = sdk.users().followers().list()
        .request(FollowersFollowingV3Query.builder()
                .username("elonmusk")
                .build())
        .call();
```

## Workflow: search tweets

```java theme={null}
String cursor = "";
while (true) {
    var page = sdk.search().advanced()
            .request(AdvancedSearchCursorQuery.builder()
                    .searchTerms(java.util.List.of("from:elonmusk AI"))
                    .sortBy("Latest")
                    .nextCursor(cursor)
                    .build())
            .call();

    // process page.tweets()

    if (!page.hasNextPage() || page.nextCursor() == null || page.nextCursor().isEmpty()) {
        break;
    }
    cursor = page.nextCursor();
}
```

## Workflow: post a tweet

Write actions need a Twitter cookie or `auth_token` on the request:

```java theme={null}
sdk.tweets().actions().create()
        .request(CreateTweetRequest.builder()
                .tweetContent("Hello from the TwexAPI Java SDK.")
                .cookie(System.getenv("TWITTER_COOKIE"))
                .build())
        .call();
```

## Error handling

The SDK throws typed exceptions for HTTP failures. Use try/catch around `.call()` and inspect status codes before retrying 4xx responses.

Add an SLF4j implementation such as Logback for debug logging during local development.

## References

* [SDKs overview](/sdks)
* [Kotlin SDK](/sdks/kotlin) (coroutine wrapper over this client)
* [Authentication](/authentication)
* [Advanced Twitter Search](/api-reference/search-endpoints/advanced-twitter-search)
* [Source Repository](https://github.com/twexapi-dev/x-api-scraper-java)
