Use the TwexAPI Java SDK to search tweets, scrape Twitter followers, and read X profiles, timelines, replies, and threads. Send DMs, search communities, fetch lists, articles, hashtags, cashtags, and global trending tweets with typed request objects. Like, retweet, follow, and post through documented REST routes. It is a Twitter API alternative for Java services and agents.
REST API | TypeScript SDK | Python SDK | Ruby SDK | Dashboard
Speakeasy generates this SDK.
| Task | REST Route | Java client |
|---|---|---|
| Search tweets without the X API | POST /twitter/advanced_search/page |
sdk.search().advanced() |
| Search hashtags or cashtags | POST /twitter/hashtags, POST /twitter/cashtags |
sdk.search().hashtags(), sdk.search().cashtags() |
| Read an X profile | GET /twitter/{screen_name}/about |
sdk.users().getAbout() |
| Read a profile timeline | GET /twitter/{screen_name}/timeline/page |
sdk.timelines().userPage() |
| Scrape Twitter followers | POST /v3/twitter/users/followers |
sdk.users().followers().list() |
| Scrape following accounts | POST /v3/twitter/users/following |
sdk.users().following().list() |
| Read tweet replies | POST /twitter/tweets/{tweet_id}/replies/page |
sdk.replies().page() |
| Read a tweet thread | POST /twitter/tweets/thread_by_id |
sdk.tweets().threadById() |
| Send or read DMs | /v3/twitter/send-dm, /v3/twitter/dm-history |
sdk.dm() |
| Search communities | POST /twitter/community/search |
sdk.communities().search() |
| Get global trending tweets | GET /twitter/global-trending/tweets |
sdk.trending().tweets() |
| Post or reply | POST /twitter/tweets/create |
sdk.tweets().actions().create() |
- Package:
io.twexapi:x-api-scraper - Source: twexapi-dev/x-api-scraper-java
- Docs: docs.twexapi.io
- License: MIT
- Dashboard: twexapi.io/dashboard
X API Scraper: Speakeasy-ready OpenAPI document for the x-api-scraper TypeScript SDK.
The SDK wraps TwexAPI's X/Twitter API surface with bearer-token authentication. Cookie/proxy based endpoints are excluded except tweet actions, follow/unfollow, and v3 DM operations. Paid engagement services, profile mutation, legacy-only operations, and non-v3 follower/following endpoints remain excluded.
Not affiliated with X Corp.
JDK 11 or later is required.
The samples below show how a published SDK artifact is used:
Gradle:
implementation 'io.twexapi:x-api-scraper:0.1.0'Maven:
<dependency>
<groupId>io.twexapi</groupId>
<artifactId>x-api-scraper</artifactId>
<version>0.1.0</version>
</dependency>Maven Central is not published yet. Install from source until it is:
./gradlew publishToMavenLocal -Pskip.signingAfter cloning the git repository to your file system you can build the SDK artifact from source to the build directory by running ./gradlew build on *nix systems or gradlew.bat on Windows systems.
If you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloning the git repo locally):
On *nix:
./gradlew publishToMavenLocal -Pskip.signingOn Windows:
gradlew.bat publishToMavenLocal -Pskip.signingpackage hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import java.lang.Exception;
import java.util.List;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of("from:elonmusk"))
.sortBy("Latest")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
}
}Get an API key from the TwexAPI dashboard. Pass it as bearerAuth, or set X_API_SCRAPER_KEY.
Write actions (tweet, follow, like, DM send) also need a Twitter cookie or auth_token on the request. Pass them on the operation input.
Keep API keys out of source code, URLs, and logs.
An asynchronous SDK client is also available that returns a CompletableFuture<T>. See Asynchronous Support for more details on async benefits and reactive library integration.
package hello.world;
import io.twexapi.sdk.AsyncXapiScraper;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.operations.async.SearchAdvancedResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class Application {
public static void main(String[] args) {
AsyncXapiScraper sdk = XapiScraper.builder()
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build()
.toAsync();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
CompletableFuture<SearchAdvancedResponse> resFut = sdk.search().advanced()
.request(req)
.call();
resFut.thenAccept(res -> {
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
});
}
}When a response field is a union model:
- Discriminated unions: branch on the discriminator (
switch) and then narrow to the concrete type. - Non-discriminated unions: use generated accessors (for example
string(),asLong(),simpleObject()) to determine the active variant.
For full model-specific examples (including Java 11/16/21 variants), see each union model's Supported Types section in the generated model docs.
The SDK provides comprehensive asynchronous support using Java's CompletableFuture<T> and Reactive Streams Publisher<T> APIs. This design makes no assumptions about your choice of reactive toolkit, allowing seamless integration with any reactive library.
Why Use Async?
Asynchronous operations provide several key benefits:
- Non-blocking I/O: Your threads stay free for other work while operations are in flight
- Better resource utilization: Handle more concurrent operations with fewer threads
- Improved scalability: Build highly responsive applications that can handle thousands of concurrent requests
- Reactive integration: Works seamlessly with reactive streams and backpressure handling
Reactive Library Integration
The SDK returns Reactive Streams Publisher<T> instances for operations dealing with streams involving multiple I/O interactions. We use Reactive Streams instead of JDK Flow API to provide broader compatibility with the reactive ecosystem, as most reactive libraries natively support Reactive Streams.
Why Reactive Streams over JDK Flow?
- Broader ecosystem compatibility: Most reactive libraries (Project Reactor, RxJava, Akka Streams, etc.) natively support Reactive Streams
- Industry standard: Reactive Streams is the de facto standard for reactive programming in Java
- Better interoperability: Seamless integration without additional adapters for most use cases
Integration with Popular Libraries:
- Project Reactor: Use
Flux.from(publisher)to convert to Reactor types - RxJava: Use
Flowable.fromPublisher(publisher)for RxJava integration - Akka Streams: Use
Source.fromPublisher(publisher)for Akka Streams integration - Vert.x: Use
ReadStream.fromPublisher(vertx, publisher)for Vert.x reactive streams - Mutiny: Use
Multi.createFrom().publisher(publisher)for Quarkus Mutiny integration
For JDK Flow API Integration: If you need JDK Flow API compatibility (e.g., for Quarkus/Mutiny 2), you can use adapters:
// Convert Reactive Streams Publisher to Flow Publisher
Flow.Publisher<T> flowPublisher = FlowAdapters.toFlowPublisher(reactiveStreamsPublisher);
// Convert Flow Publisher to Reactive Streams Publisher
Publisher<T> reactiveStreamsPublisher = FlowAdapters.toPublisher(flowPublisher);For standard single-response operations, the SDK returns CompletableFuture<T> for straightforward async execution.
Supported Operations
Async support is available for:
- Server-sent Events: Stream real-time events with Reactive Streams
Publisher<T> - JSONL Streaming: Process streaming JSON lines asynchronously
- Pagination: Iterate through paginated results using
callAsPublisher()andcallAsPublisherUnwrapped() - File Uploads: Upload files asynchronously with progress tracking
- File Downloads: Download files asynchronously with streaming support
- Standard Operations: All regular API calls return
CompletableFuture<T>for async execution
This SDK supports the following security scheme globally:
| Name | Type | Scheme |
|---|---|---|
bearerAuth |
http | HTTP Bearer |
To authenticate with the API the bearerAuth parameter must be set when initializing the SDK client instance. For example:
package hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import java.lang.Exception;
import java.util.List;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
}
}Available methods
- balance - Get Balance
- sentiment - Sentiment Analysis
- members - Get Community Members
- membersPage - Get Community Members by Page
- tweets - Get Community Tweets
- tweetsPage - Get Community Tweets by Page
- search - Search Community
- get - Get Community
- searchTweets - Search Community Tweets
- status - Check DM Permissions
- send - Send DM
- history - Get DM History
- media - Get DM Media
- conversations - Get Conversations
- tweets - Get List Tweets
- tweetsPage - Get List Tweets by Page
- subscribers - Get List Subscribers
- members - Get List Members
- membersPage - Get List Members by Page
- search - Search List
- tweetsAndReplies - Get All Tweets and Replies by User
- userPage - Get User Timeline by Page
- user - Get User Timeline and Fill Count
- tweetsAndRepliesPage - Get All Tweets and Replies by User by Page
- countries - List Global Trend Countries
- topics - List Global Trend Topics
- contents - List Global Trend Content Tags
- tweets - Get Global Trending Tweets
- byCountry - Get Trending Topics
- detail - Get Tweet Detail
- thread - Get Tweet Thread by ID
- lookup - Batch Get Tweets by ID
- similar - Get Similar Tweets
- like - Like a Tweet
- unlike - Unlike a Tweet
- retweet - Retweet a Tweet
- unretweet - Delete Retweet
- createThread - Create a Tweet Thread
- create - Create a Tweet or Reply
- quote - Create a Quote Tweet
- createWithoutCookie - Post Tweet (Auto Cookie)
- bookmark - Bookmark a Tweet
- unbookmark - Delete Bookmark
- deleteBatch - Delete One or More Tweets
- retweeters - Get Retweeters
- retweetersPage - Get Retweeters by Page
- quotes - Get Quote Tweets
- quotesPage - Get Quote Tweets by Page
- page - Get Replies by Page
- getByUsernames - Get Multiple Users by Usernames
- getByIds - Users Details by ID
- verifyAccount - Verify Account Status
- getStatuses - Batch Get User account status
- search - Search User
- follow - Follow User
- unfollow - Unfollow User
- getAccountBased - Get Twitter Account Based in
- getAbout - Get Twitter User About by Screen Name
- list - Get Following (v3)
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, you can provide a RetryConfig object through the retryConfig builder method:
package hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import io.twexapi.sdk.utils.BackoffStrategy;
import io.twexapi.sdk.utils.RetryConfig;
import java.lang.Exception;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.retryConfig(RetryConfig.builder()
.backoff(BackoffStrategy.builder()
.initialInterval(1L, TimeUnit.MILLISECONDS)
.maxInterval(50L, TimeUnit.MILLISECONDS)
.maxElapsedTime(1000L, TimeUnit.MILLISECONDS)
.baseFactor(1.1)
.jitterFactor(0.15)
.retryConnectError(false)
.build())
.build())
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
}
}If you'd like to override the default retry strategy for all operations that support retries, you can provide a configuration at SDK initialization:
package hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import io.twexapi.sdk.utils.BackoffStrategy;
import io.twexapi.sdk.utils.RetryConfig;
import java.lang.Exception;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.retryConfig(RetryConfig.builder()
.backoff(BackoffStrategy.builder()
.initialInterval(1L, TimeUnit.MILLISECONDS)
.maxInterval(50L, TimeUnit.MILLISECONDS)
.maxElapsedTime(1000L, TimeUnit.MILLISECONDS)
.baseFactor(1.1)
.jitterFactor(0.15)
.retryConnectError(false)
.build())
.build())
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
}
}Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception.
XapiScraperException is the base class for all HTTP error responses. It has the following properties:
| Method | Type | Description |
|---|---|---|
message() |
String |
Error message |
code() |
int |
HTTP response status code eg 404 |
headers |
Map<String, List<String>> |
HTTP response headers |
body() |
byte[] |
HTTP body as a byte array. Can be empty array if no body is returned. |
bodyAsString() |
String |
HTTP body as a UTF-8 string. Can be empty string if no body is returned. |
rawResponse() |
HttpResponse<?> |
Raw HTTP response (body already read and not available for re-read) |
package hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.components.ValidationError;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.errors.XapiScraperException;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import java.io.UncheckedIOException;
import java.lang.Exception;
import java.util.List;
import java.util.Optional;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
try {
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
} catch (XapiScraperException ex) { // all SDK exceptions inherit from XapiScraperException
// ex.ToString() provides a detailed error message including
// HTTP status code, headers, and error payload (if any)
System.out.println(ex);
// Base exception fields
var rawResponse = ex.rawResponse();
var headers = ex.headers();
var contentType = headers.first("Content-Type");
int statusCode = ex.code();
Optional<byte[]> responseBody = ex.body();
// different error subclasses may be thrown
// depending on the service call
if (ex instanceof HTTPValidationError) {
var e = (HTTPValidationError) ex;
// Check error data fields
e.data().ifPresent(payload -> {
Optional<List<ValidationError>> detail = payload.detail();
});
}
// An underlying cause may be provided. If the error payload
// cannot be deserialized then the deserialization exception
// will be set as the cause.
if (ex.getCause() != null) {
var cause = ex.getCause();
}
} catch (UncheckedIOException ex) {
// handle IO error (connection, timeout, etc)
} }
}Primary errors:
XapiScraperException: The base class for HTTP error responses.io.twexapi.sdk.models.errors.HTTPValidationError: Validation Error. Status code422. *
Less common errors (6)
Network errors:
java.io.IOException(always wrapped byjava.io.UncheckedIOException). Commonly encountered subclasses ofIOExceptionincludejava.net.ConnectException,java.net.SocketTimeoutException,EOFException(there are many more subclasses in the JDK platform).
Inherit from XapiScraperException:
* Check the method documentation to see if the error is applicable.
You can override the default server globally using the .server(AvailableServers server) builder method when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:
| Name | Server | Description |
|---|---|---|
production |
https://api.twexapi.io |
TwexAPI production API |
package hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import java.lang.Exception;
import java.util.List;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.server(XapiScraper.AvailableServers.PRODUCTION)
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
}
}The default server can also be overridden globally using the .serverURL(String serverUrl) builder method when initializing the SDK client instance. For example:
package hello.world;
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.models.components.AdvancedSearchCursorQuery;
import io.twexapi.sdk.models.errors.HTTPValidationError;
import io.twexapi.sdk.models.operations.SearchAdvancedResponse;
import java.lang.Exception;
import java.util.List;
public class Application {
public static void main(String[] args) throws HTTPValidationError, Exception {
XapiScraper sdk = XapiScraper.builder()
.serverURL("https://api.twexapi.io")
.bearerAuth(System.getenv("X_API_SCRAPER_KEY"))
.build();
AdvancedSearchCursorQuery req = AdvancedSearchCursorQuery.builder()
.searchTerms(List.of(
"<value 1>",
"<value 2>",
"<value 3>"))
.sortBy("<value>")
.nextCursor("")
.build();
SearchAdvancedResponse res = sdk.search().advanced()
.request(req)
.call();
if (res.advancedSearchCursorResponse().isPresent()) {
System.out.println(res.advancedSearchCursorResponse().get());
}
}
}The Java SDK makes API calls using an HTTPClient that wraps the native
HttpClient. This
client provides the ability to attach hooks around the request lifecycle that can be used to modify the request or handle
errors and response.
The HTTPClient interface allows you to either use the default SpeakeasyHTTPClient that comes with the SDK,
or provide your own custom implementation with customized configuration such as custom executors, SSL context,
connection pools, and other HTTP client settings.
The interface provides synchronous (send) methods and asynchronous (sendAsync) methods. The sendAsync method
is used to power the async SDK methods and returns a CompletableFuture<HttpResponse<Blob>> for non-blocking operations.
The following example shows how to add a custom header and handle errors:
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.utils.HTTPClient;
import io.twexapi.sdk.utils.SpeakeasyHTTPClient;
import io.twexapi.sdk.utils.Utils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.InputStream;
import java.time.Duration;
public class Application {
public static void main(String[] args) {
// Create a custom HTTP client with hooks
HTTPClient httpClient = new HTTPClient() {
private final HTTPClient defaultClient = new SpeakeasyHTTPClient();
@Override
public HttpResponse<InputStream> send(HttpRequest request) throws IOException, URISyntaxException, InterruptedException {
// Add custom header and timeout using Utils.copy()
HttpRequest modifiedRequest = Utils.copy(request)
.header("x-custom-header", "custom value")
.timeout(Duration.ofSeconds(30))
.build();
try {
HttpResponse<InputStream> response = defaultClient.send(modifiedRequest);
// Log successful response
System.out.println("Request successful: " + response.statusCode());
return response;
} catch (Exception error) {
// Log error
System.err.println("Request failed: " + error.getMessage());
throw error;
}
}
};
XapiScraper sdk = XapiScraper.builder()
.client(httpClient)
.build();
}
}Custom HTTP Client Configuration
You can also provide a completely custom HTTP client with your own configuration:
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.utils.HTTPClient;
import io.twexapi.sdk.utils.Blob;
import io.twexapi.sdk.utils.ResponseWithBody;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.InputStream;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.CompletableFuture;
public class Application {
public static void main(String[] args) {
// Custom HTTP client with custom configuration
HTTPClient customHttpClient = new HTTPClient() {
private final HttpClient client = HttpClient.newBuilder()
.executor(Executors.newFixedThreadPool(10))
.connectTimeout(Duration.ofSeconds(30))
// .sslContext(customSslContext) // Add custom SSL context if needed
.build();
@Override
public HttpResponse<InputStream> send(HttpRequest request) throws IOException, URISyntaxException, InterruptedException {
return client.send(request, HttpResponse.BodyHandlers.ofInputStream());
}
@Override
public CompletableFuture<HttpResponse<Blob>> sendAsync(HttpRequest request) {
// Convert response to HttpResponse<Blob> for async operations
return client.sendAsync(request, HttpResponse.BodyHandlers.ofPublisher())
.thenApply(resp -> new ResponseWithBody<>(resp, Blob::from));
}
};
XapiScraper sdk = XapiScraper.builder()
.client(customHttpClient)
.build();
}
}You can also enable debug logging on the default SpeakeasyHTTPClient:
import io.twexapi.sdk.XapiScraper;
import io.twexapi.sdk.utils.SpeakeasyHTTPClient;
public class Application {
public static void main(String[] args) {
SpeakeasyHTTPClient httpClient = new SpeakeasyHTTPClient();
httpClient.enableDebugLogging(true);
XapiScraper sdk = XapiScraper.builder()
.client(httpClient)
.build();
}
}This SDK uses SLF4j for structured logging across HTTP requests, retries, pagination, streaming, and hooks. SLF4j provides comprehensive visibility into SDK operations.
Log Levels:
- DEBUG: High-level operations (HTTP requests/responses, retry attempts, page fetches, hook execution, stream lifecycle)
- TRACE: Detailed information (request/response bodies, backoff calculations, individual items processed)
Configuration:
Add your preferred SLF4j implementation to your project. For example, using Logback:
dependencies {
implementation 'ch.qos.logback:logback-classic:1.4.14'
}Configure logging levels in your logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- SDK-wide logging -->
<logger name="io.twexapi.sdk" level="DEBUG"/>
<!-- Component-specific logging -->
<logger name="io.twexapi.sdk.utils.SpeakeasyHTTPClient" level="DEBUG"/>
<logger name="io.twexapi.sdk.utils.Retries" level="DEBUG"/>
<logger name="io.twexapi.sdk.utils.pagination" level="DEBUG"/>
<logger name="io.twexapi.sdk.utils.Hooks" level="TRACE"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>What Gets Logged:
- HTTP Client: Request/response details, headers (with sensitive headers redacted), bodies (at TRACE level)
- Retries: Retry attempts, backoff delays, exhaustion, non-retryable exceptions
- Pagination: Page fetches, pagination state, errors
- Streaming: Stream initialization, item processing, closure
- Hooks: Hook execution counts, operation IDs, exceptions
For backward compatibility, you can still use the legacy debug logging method:
SDK.builder()
.enableHTTPDebugLogging(true)
.build();Example output:
Sending request: http://localhost:35123/bearer#global GET
Request headers: {Accept=[application/json], Authorization=[******], Client-Level-Header=[added by client], Idempotency-Key=[some-key], x-speakeasy-user-agent=[speakeasy-sdk/java 0.0.1 internal 0.1.0 org.openapis.openapi]}
Received response: (GET http://localhost:35123/bearer#global) 200
Response headers: {access-control-allow-credentials=[true], access-control-allow-origin=[*], connection=[keep-alive], content-length=[50], content-type=[application/json], date=[Wed, 09 Apr 2025 01:43:29 GMT], server=[gunicorn/19.9.0]}
Response body:
{
"authenticated": true,
"token": "global"
}
WARNING: Debug logging should only be used for temporary debugging purposes. Leaving this option on in a production system could expose credentials/secrets in logs. Authorization headers are redacted by default. You can specify additional redacted header names via SpeakeasyHTTPClient.setRedactedHeaders.
NOTE: This is a convenience method that calls HTTPClient.enableDebugLogging(). The SpeakeasyHTTPClient honors this setting. If you are using a custom HTTP client, it is up to the custom client to honor this setting.
Another option is to set the System property -Djdk.httpclient.HttpClient.log=all. However, this option does not log request/response bodies.
The SDK ships with a pre-configured Jackson ObjectMapper accessible via
JSON.getMapper(). It is set up with type modules, strict deserializers, and the feature flags
needed for full SDK compatibility (including ISO-8601 OffsetDateTime serialization):
import io.twexapi.sdk.utils.JSON;
String json = JSON.getMapper().writeValueAsString(response);To compose with your own ObjectMapper, register the provided XApiScraperJacksonModule, which
bundles all the same modules and feature flags as a single plug-and-play module:
import io.twexapi.sdk.utils.XApiScraperJacksonModule;
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper myMapper = new ObjectMapper()
.registerModule(new XApiScraperJacksonModule());
String json = myMapper.writeValueAsString(response);This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
To regenerate from the OpenAPI document:
speakeasy runThe source spec lives in openapi.sdk.json. Keep it in sync with x-api-scraper-typescript.
GitHub Actions can regenerate the SDK and publish to Maven Central. Add these repository secrets first:
SPEAKEASY_API_KEYOSSRH_USERNAMEOSSRH_PASSWORDJAVA_GPG_SECRET_KEYJAVA_GPG_PASSPHRASE
TwexAPI is an independent third-party service. Not affiliated with X Corp. "Twitter" and "X" are trademarks of X Corp.