Use the TwexAPI Go 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 Go services and agents.
REST API | TypeScript SDK | Python SDK | Ruby SDK | Java SDK | Kotlin SDK | C# SDK | PHP SDK | Dashboard
Speakeasy generates this SDK.
| Task | REST Route | Go client |
|---|---|---|
| Search tweets without the X API | POST /twitter/advanced_search/page |
s.Search.Advanced(...) |
| Search hashtags or cashtags | POST /twitter/hashtags, POST /twitter/cashtags |
s.Search.Hashtags(), s.Search.Cashtags() |
| Read an X profile | GET /twitter/{screen_name}/about |
s.Users.GetAbout(...) |
| Read a profile timeline | GET /twitter/{screen_name}/timeline/page |
s.Timelines.UserPage(...) |
| Scrape Twitter followers | POST /v3/twitter/users/followers |
s.Users.Followers.List(...) |
| Scrape following accounts | POST /v3/twitter/users/following |
s.Users.Following.List(...) |
| Read tweet replies | POST /twitter/tweets/{tweet_id}/replies/page |
s.Tweets.Replies.Page(...) |
| Read a tweet thread | POST /twitter/tweets/thread_by_id |
s.Tweets.Thread(...) |
| Send or read DMs | /v3/twitter/send-dm, /v3/twitter/dm-history |
s.Dm |
| Search communities | POST /twitter/community/search |
s.Communities.Search(...) |
| Get global trending tweets | GET /twitter/global-trending/tweets |
s.Trending.Tweets(...) |
| Post or reply | POST /twitter/tweets/create |
s.Tweets.Actions.Create(...) |
- Module:
github.com/twexapi-dev/x-api-scraper-go - Source: twexapi-dev/x-api-scraper-go
- Docs: docs.twexapi.io · pkg.go.dev
- 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.
go get github.com/twexapi-dev/x-api-scraper-go@v0.1.0package main
import (
"context"
"log"
"os"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_KEY")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{"from:elonmusk"},
SortBy: "Latest",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
})
if err != nil {
log.Fatal(err)
}
log.Printf("%+v\n", res)
}Get an API key from the TwexAPI dashboard. Pass it to WithSecurity(), or set X_API_SCRAPER_KEY. The generated client also reads X_API_SCRAPER_BEARER_AUTH.
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.
This SDK supports the following security scheme globally:
| Name | Type | Scheme | Environment Variable |
|---|---|---|---|
BearerAuth |
http | HTTP Bearer | X_API_SCRAPER_BEARER_AUTH |
You can configure it using the WithSecurity option when initializing the SDK client instance. For example:
package main
import (
"context"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
"log"
"os"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_BEARER_AUTH")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{
"<value 1>",
"<value 2>",
"<value 3>",
},
SortBy: "<value>",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
})
if err != nil {
log.Fatal(err)
}
if res.AdvancedSearchCursorResponse != nil {
// handle response
}
}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, simply provide a retry.Config object to the call by using the WithRetries option:
package main
import (
"context"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
"github.com/twexapi-dev/x-api-scraper-go/retry"
"log"
"models/operations"
"os"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_BEARER_AUTH")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{
"<value 1>",
"<value 2>",
"<value 3>",
},
SortBy: "<value>",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
}, operations.WithRetries(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}))
if err != nil {
log.Fatal(err)
}
if res.AdvancedSearchCursorResponse != nil {
// handle response
}
}If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:
package main
import (
"context"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
"github.com/twexapi-dev/x-api-scraper-go/retry"
"log"
"os"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithRetryConfig(
retry.Config{
Strategy: "backoff",
Backoff: &retry.BackoffStrategy{
InitialInterval: 1,
MaxInterval: 50,
Exponent: 1.1,
MaxElapsedTime: 100,
},
RetryConnectionErrors: false,
}),
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_BEARER_AUTH")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{
"<value 1>",
"<value 2>",
"<value 3>",
},
SortBy: "<value>",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
})
if err != nil {
log.Fatal(err)
}
if res.AdvancedSearchCursorResponse != nil {
// handle response
}
}Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.
By Default, an API error will return apierrors.APIError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.
For example, the Advanced function may return the following errors:
| Error Type | Status Code | Content Type |
|---|---|---|
| apierrors.HTTPValidationError | 422 | application/json |
| apierrors.APIError | 4XX, 5XX | */* |
package main
import (
"context"
"errors"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/apierrors"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
"log"
"os"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_BEARER_AUTH")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{
"<value 1>",
"<value 2>",
"<value 3>",
},
SortBy: "<value>",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
})
if err != nil {
var e *apierrors.HTTPValidationError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *apierrors.APIError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
}
}You can override the default server globally using the WithServer(server string) option 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 main
import (
"context"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
"log"
"os"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithServer("production"),
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_BEARER_AUTH")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{
"<value 1>",
"<value 2>",
"<value 3>",
},
SortBy: "<value>",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
})
if err != nil {
log.Fatal(err)
}
if res.AdvancedSearchCursorResponse != nil {
// handle response
}
}The default server can also be overridden globally using the WithServerURL(serverURL string) option when initializing the SDK client instance. For example:
package main
import (
"context"
xapiscraper "github.com/twexapi-dev/x-api-scraper-go"
"github.com/twexapi-dev/x-api-scraper-go/models/components"
"github.com/twexapi-dev/x-api-scraper-go/optionalnullable"
"log"
"os"
)
func main() {
ctx := context.Background()
s := xapiscraper.New(
xapiscraper.WithServerURL("https://api.twexapi.io"),
xapiscraper.WithSecurity(os.Getenv("X_API_SCRAPER_BEARER_AUTH")),
)
res, err := s.Search.Advanced(ctx, components.AdvancedSearchCursorQuery{
SearchTerms: []string{
"<value 1>",
"<value 2>",
"<value 3>",
},
SortBy: "<value>",
NextCursor: optionalnullable.From(xapiscraper.Pointer("")),
})
if err != nil {
log.Fatal(err)
}
if res.AdvancedSearchCursorResponse != nil {
// handle response
}
}The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.
import (
"net/http"
"time"
"github.com/twexapi-dev/x-api-scraper-go"
)
var (
httpClient = &http.Client{Timeout: 30 * time.Second}
sdkClient = xapiscraper.New(xapiscraper.WithClient(httpClient))
)This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.
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.