Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TwexAPI Go SDK: Twitter API for search, followers, DMs & X automation

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.

Common Twitter & X tasks

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(...)

Package & registry trust

Summary

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.

Table of Contents

SDK Installation

go get github.com/twexapi-dev/x-api-scraper-go@v0.1.0

SDK Example Usage

Example

package 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.

Authentication

Per-Client Security Schemes

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 Resources and Operations

Available methods
  • Fetch - Batch Fetch X Articles
  • Markdown - Fetch Article as Markdown
  • Page - Get Replies by Page
  • List - Get Followers (v3)
  • Verified - Get Verified Followers
  • List - Get Following (v3)

Retries

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
	}
}

Error Handling

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 */*

Example

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())
		}
	}
}

Server Selection

Select Server by Name

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

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.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
	}
}

Override Server URL Per-Client

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
	}
}

Custom HTTP Client

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.

Development

Maturity

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.

Contributions

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.

SDK Created by Speakeasy

About

Go SDK for Twitter search, followers, DMs, communities & X automation. Built for TwexAPI. Not affiliated with X Corp.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages