Official C# / .NET 8+ sample code for the X API v2 powered by the official XDK for C# (XDevPlatform.Xdk).
- .NET 8.0 SDK or later (
dotnet --version) - An X Developer Account with API credentials
Add XDevPlatform.Xdk to your project (already configured in csharp/XdkSamples.csproj):
dotnet add package XDevPlatform.XdkBuild all 80 samples at once:
cd csharp
dotnet buildList all available samples:
dotnet run -- --helpexport BEARER_TOKEN="your_bearer_token"
dotnet run -- posts/search_recentSamples that act on behalf of a user (creating posts, managing lists/bookmarks/likes/DMs, media upload) support either a pre-generated ACCESS_TOKEN or interactive OAuth 2.0 PKCE via CLIENT_ID (and optional CLIENT_SECRET):
export CLIENT_ID="your_oauth2_client_id"
export CLIENT_SECRET="your_oauth2_client_secret" # optional for public clients
export REDIRECT_URI="https://example.com" # must match your App settings
dotnet run -- posts/create_post| Category | Samples | Example Command |
|---|---|---|
activity/ |
5 | dotnet run -- activity/list_subscriptions |
compliance/ |
5 | dotnet run -- compliance/get_jobs |
direct_messages/ |
6 | dotnet run -- direct_messages/get_user_conversation_events |
lists/ |
9 | dotnet run -- lists/get_list_by_id |
media/ |
2 | dotnet run -- media/media_upload_v2 |
posts/ |
12 | dotnet run -- posts/search_recent |
spaces/ |
2 | dotnet run -- spaces/search_spaces |
streams/ |
2 | dotnet run -- streams/stream_posts_filtered |
usage/ |
1 | dotnet run -- usage/get_usage |
users/ |
31 | dotnet run -- users/get_users_by_usernames |
webhooks/ |
5 | dotnet run -- webhooks/list_webhooks |
Every paginated endpoint exposes a *Pages(...) helper returning Paginator<T> : IAsyncEnumerable<T>:
var options = new PostsClient.SearchRecentOptions
{
MaxResults = 100,
PostFields = new List<string> { "author_id", "created_at" },
};
await foreach (var page in client.Posts.SearchRecentPages("from:XDevelopers", options))
{
foreach (var post in page.Data ?? Enumerable.Empty<Post>())
{
Console.WriteLine($"{post.Id}: {post.Text}");
}
}Streaming endpoints return EventStream<T> : IAsyncEnumerable<T>, IAsyncDisposable:
await using var stream = client.Stream.PostsStream();
await foreach (var evt in stream)
{
Console.WriteLine(evt.Data?.Text);
}