SDKs
Introduction
A software development kit (SDK) is a set of software tools and programs tailored for a specific platform or API. The purpose of an SDK is to build or extend functionality of applications by providing libraries or codebases that developers can use within their applications, efficiently and with minimal coding. This significantly speeds up the development process, saving time, money, and effort.
The X Developer Platform now offers 2 official SDKs for those who develop in TypeScript/Javascript and Java. These will allow developers to build more effectively by eliminating the need to manually program the complexities around the X API v2, utilizing the pre-built functions for all available v2 endpoints as well as simplifying the authentication process. As these are built and maintained by the Developer Platform team, they will always be up to date with future releases to the X API v2
Since these SDKs wrap the X API, you must have a developer account to authenticate requests using the credentials from a developer App, located within a Project.
Installing
There are a few ways to install the Java package (requires Java 1.8+)
- Maven users - add this dependency to your project’s POM file:
-
Grade users - add this dependency to your project’s build file:
implementation "com.twitter:twitter-api-java-sdk:1.1.4"
-
Others - first generate the JAR by running the following command
mvn clean package
Then manually install the following JARs:
target/twitter-api-java-sdk-1.1.4.jar
target/lib/*.jar
Client basics
Import the classes (Java) and package (TypeScript) at the top of a working file to gain access to the authentication and library clients. In order to use the methods from the library client, authentication credentials must be passed, this could either be Bearer Token (App-only) or client id/client secret if authenticating with OAuth 2.0 user context.
Here are examples of how this would look:
Authentication Flow
If you are using the application only option to authenticate the SDKs, you will only need to provide the token and the library client will be ready to use the endpoint methods right away. Keep in mind, application only tokens cannot be used on endpoints that require user context authentication.
OAuth 2.0 user context authentication requires a few extra steps after creating the auth client.
- Generate authorization URL
- Authorize the application from the authorization URL
- Redirects to callback (this should be matching the callback URL set in the auth settings page in the Developer Portal).
- Parse code verifier to exchange for access token
The SDKs provide methods on the auth client that simplifies these steps. For a full example of how to make a request authenticating with OAuth 2.0 user context, check out the GitHub repositories.
Endpoint methods
The methods provided within the library client are clearly named to correspond with every endpoint and all parameters are passed in as arguments. Here is an example of Post lookup by ID: