{"id":15293325,"url":"https://github.com/tomholford/twitter-labs-api","last_synced_at":"2025-04-13T13:31:36.321Z","repository":{"id":39687132,"uuid":"244545122","full_name":"tomholford/twitter-labs-api","owner":"tomholford","description":"A basic implementation of a Twitter Labs API client in Ruby","archived":false,"fork":false,"pushed_at":"2023-03-17T02:38:12.000Z","size":57,"stargazers_count":7,"open_issues_count":4,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T04:51:24.116Z","etag":null,"topics":["gem","ruby","ruby-gem","twitter","twitter-api"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/twitter_labs_api","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomholford.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-03T04:54:50.000Z","updated_at":"2023-01-09T05:07:51.000Z","dependencies_parsed_at":"2023-02-12T06:16:05.790Z","dependency_job_id":null,"html_url":"https://github.com/tomholford/twitter-labs-api","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomholford%2Ftwitter-labs-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomholford%2Ftwitter-labs-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomholford%2Ftwitter-labs-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomholford%2Ftwitter-labs-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomholford","download_url":"https://codeload.github.com/tomholford/twitter-labs-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248571894,"owners_count":21126522,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","ruby","ruby-gem","twitter","twitter-api"],"created_at":"2024-09-30T16:46:18.130Z","updated_at":"2025-04-13T13:31:31.309Z","avatar_url":"https://github.com/tomholford.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# twitter-labs-api\n\n[![Labs v2](https://img.shields.io/static/v1?label=Twitter%20API\u0026message=Developer%20Labs%20v2\u0026color=192430\u0026style=flat\u0026logo=Twitter)](https://developer.twitter.com/en/docs/labs/overview/versioning)\n\nA basic implementation of a Twitter Labs API client as a handy Ruby [gem](https://rubygems.org/gems/twitter_labs_api). This project uses the v2 endpoints announced [here](https://twittercommunity.com/t/releasing-a-new-version-of-labs-endpoints/134219/3).\n\n## Usage\n\n### Prerequisite\nAll one needs is a Twitter [bearer token](https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0/bearer-tokens) to get started. The bearer token is available on the 'Tokens and Keys' page within your app's dashboard on the [Twitter for Developers](https://developer.twitter.com/) site.\n\nAlternatively, one can get a bearer token using [this method](https://www.rubydoc.info/gems/twitter/Twitter/REST/Client#bearer_token%3F-instance_method) from https://github.com/sferik/twitter.\n\n### Setup\n\n```shell\ngem install twitter_labs_api\n```\n\n### Example\n\n#### Getting a Tweet by ID\n```ruby\nrequire `twitter_labs_api`\n\napi = TwitterLabsAPI.new(bearer_token: 'YOUR-BEARER-TOKEN')\n\napi.get_tweet(id: '1234671272602193920')\n\n\u003e\u003e {\"data\"=\u003e{\"author_id\"=\u003e\"44196397\", \"created_at\"=\u003e\"2020-03-03T02:45:45.000Z\", \"id\"=\u003e\"1234671272602193920\", \"lang\"=\u003e\"und\", \"public_metrics\"=\u003e{\"retweet_count\"=\u003e4534, \"reply_count\"=\u003e1036, \"like_count\"=\u003e43489, \"quote_count\"=\u003e224}, \"text\"=\u003e\"✌️ bro https://t.co/nJ7CUyhr2j\"}}\n```\n\n#### Specifying which fields to include in the response\n\nBy default, the gem requests the 'default' fields for each entity. See the [API Reference](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference) for available fields. One can customize the response payload, depending on the requested resource.\n\nFor example, to request the URL of a Tweet's embedded media item:\n```ruby\nrequested_fields = { tweet: %w[id username], media: %w[url] }\n\napi.get_tweet(id: my_id, fields: requested_fields)\n```\n\n#### API Errors\n\nSometimes the API will respond with an error, for example, `429 Too Many Requests`. The gem will throw an error with the `Net::HTTP` response as an attribute for proper exception-handling by the consuming app:\n\n```ruby\ndef my_twitter_request\n  api.get_tweet(id: '1235508591232090112', tweet_fields: my_fields)\n\nrescue TwitterLabsAPI::APIError =\u003e e\n  puts e.msg # 429 Too Many Requests\n  puts e.response # \u003cNet::HTTPTooManyRequests 429 Too Many Requests readbody=true\u003e\n  # do something with the Net::HTTP response...\nend\n```\n\n### Status\nCurrently, the following endpoints are implemented:\n\n#### Tweets\n\n- `TwitterLabsAPI#get_tweet` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-tweets-id)) - Retrieve a single Tweet object with an `id`\n- `TwitterLabsAPI#get_tweets` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-tweets)) - Retrieve multiple Tweets with a collection of `ids`\n- `TwitterLabsAPI#hide_reply` ([docs](https://developer.twitter.com/en/docs/labs/hide-replies/api-reference/put-hidden)) - Hide a reply by referencing it's `id`; must be in a conversation belonging to the authenticating user\n- `TwitterLabsAPI#search` ([docs](https://developer.twitter.com/en/docs/labs/recent-search/api-reference/get-recent-search)) - Returns Tweets from the last 7 days that match a search query.\n\n#### Users\n\n- `TwitterLabsAPI#get_user` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-users-id)) - Retrieve a single user object with an `id`\n- `TwitterLabsAPI#get_users` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-users)) - Retrieve multiple user objects with a collection of `ids`\n- `TwitterLabsAPI#get_users_by_username` ([docs](https://developer.twitter.com/en/docs/labs/tweets-and-users/api-reference/get-users)) - Retrieve multiple user objects with a collection of `usernames`\n\n## Roadmap\n\nCurrently focused on implementing support for all v2 endpoints; if there is enough interest, I will add v1 endpoint support as well.\n\nAnd of course, contributions are welcome :)\n\n## Dependencies\n\nThis lib uses Ruby's built-in `URI` and `net/http` libs to communicate with Twitter's Labs API.\n\nFor ease of manipulating responses, this lib depends on `Hash::WithIndifferentAccess` from the Rails `activesupport` project ([docs](https://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html)).\n\nThus, one can access the data from a response like so:\n```ruby\nresponse = api.get_tweet(id: '1234671272602193920')\n\nputs response[:data][:public_metrics][:like_count]\n\u003e\u003e 43489\n\nputs response['data']['public_metrics']['like_count']\n\u003e\u003e 43489\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomholford%2Ftwitter-labs-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomholford%2Ftwitter-labs-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomholford%2Ftwitter-labs-api/lists"}