SDKs overview
Gitter provides a RESTful API that allows developers to interact with its messaging platform programmatically. This API enables functionalities such as sending and receiving messages, managing chat rooms, and retrieving user information. While Gitter maintains a core API for these interactions, the development of official, standalone SDKs has evolved, particularly following Gitter's integration with the Matrix.org ecosystem. Developers typically rely on Gitter's official API documentation to build custom integrations or utilize community-contributed libraries.
The Gitter API is designed to be accessible via standard HTTP requests, returning data primarily in JSON format. Authentication for the API typically uses OAuth2 tokens, which can be obtained by registering an application with Gitter. This approach aligns with common practices for securing web APIs, as detailed in the OAuth 2.0 specification. The API supports various endpoints for different resources, including /rooms, /users, and /messages, enabling comprehensive control over Gitter's core features.
Official SDKs by language
While Gitter itself has focused on an open API approach rather than a broad suite of officially maintained SDKs for every language, it does provide specific tools and official documentation that serve as the foundation for integrations. The primary official resource for developers is the Gitter Developer documentation, which details the API endpoints and authentication flow. For practical client-side interaction, Gitter provides a JavaScript client library.
The following table outlines the key official client library and its details:
| Language | Package/Library | Maturity | Description |
|---|---|---|---|
| JavaScript (Node.js/Browser) | gitter-client |
Stable | Official JavaScript client for interacting with the Gitter API. Provides methods for rooms, messages, and user operations. |
Installation
Installing the official Gitter JavaScript client typically involves using npm, the Node.js package manager. This package facilitates communication with the Gitter API by handling HTTP requests and authentication boilerplate.
JavaScript (Node.js/Browser)
To install the gitter-client, open your terminal or command prompt and run the following command:
npm install gitter-client
This command adds the gitter-client package to your project's dependencies, making its functionalities available for use in your JavaScript or Node.js application.
After installation, you can import the library into your project:
const Gitter = require('gitter-client');
// or for ES Modules:
// import Gitter from 'gitter-client';
Quickstart example
This quickstart example demonstrates how to use the official gitter-client to fetch a list of public rooms and send a message to a specific room. Before running this code, ensure you have obtained a Gitter access token. You can generate an access token by registering an application in your Gitter settings.
Replace 'YOUR_ACCESS_TOKEN' with your actual Gitter API access token and 'YOUR_ROOM_ID' with the ID of the room you wish to send a message to. The room ID can often be found in the URL of the Gitter room or by calling the API's /rooms endpoint.
const Gitter = require('gitter-client');
const accessToken = 'YOUR_ACCESS_TOKEN'; // Replace with your Gitter access token
const roomId = 'YOUR_ROOM_ID'; // Replace with the ID of the room you want to interact with
const gitter = new Gitter(accessToken);
// Function to list public rooms
async function listPublicRooms() {
try {
console.log('Fetching public rooms...');
const rooms = await gitter.rooms.get();
console.log('Public Rooms:');
rooms.forEach(room => {
if (room.public) {
console.log(`- ${room.name} (ID: ${room.id})`);
}
});
} catch (error) {
console.error('Error fetching public rooms:', error.message);
}
}
// Function to send a message to a specific room
async function sendMessageToRoom(targetRoomId, messageText) {
try {
console.log(`Sending message to room ${targetRoomId}...`);
const message = await gitter.rooms.sendChatMessage(targetRoomId, messageText);
console.log('Message sent successfully:', message.text);
} catch (error) {
console.error('Error sending message:', error.message);
}
}
// Execute the functions
(async () => {
await listPublicRooms();
// To send a message, uncomment the line below and ensure you have a valid roomId and message.
// await sendMessageToRoom(roomId, 'Hello from Gitter SDK Quickstart!');
})();
This example initializes the Gitter client with your access token, then demonstrates how to retrieve a list of public rooms. The commented-out sendMessageToRoom function illustrates how to post a message, providing a clear path for further development. Remember to handle your access token securely, avoiding hardcoding it in production environments.
Community libraries
Due to Gitter's open API and its long-standing presence in the developer community, various community-contributed libraries and wrappers have emerged over time. These libraries often provide abstractions for different programming languages, making it easier to integrate with the Gitter API without directly handling HTTP requests and JSON parsing.
While Gitter does not officially endorse or maintain these libraries, they can be valuable resources for developers working in specific language ecosystems. Common languages for which community libraries might exist include Python, Ruby, and Go, among others. These libraries typically wrap the core Gitter REST API endpoints, offering language-specific methods for common operations like:
- User authentication: Simplifying the OAuth2 flow.
- Room management: Creating, joining, leaving, and listing rooms.
- Message handling: Sending, receiving, and parsing chat messages.
- User interaction: Retrieving user profiles and managing user presence.
Developers looking for community libraries often search package repositories specific to their language (e.g., PyPI for Python, RubyGems for Ruby) using terms like gitter api or gitter client. When choosing a community library, it is advisable to check its documentation, the date of its last update, and its community support to ensure it is actively maintained and compatible with the current Gitter API versions. The GitterHQ/developers room on Gitter can also be a good place to ask for recommendations or find discussions about community-maintained tools.
The Gitter API's design, which uses standard HTTP methods and JSON payloads, also facilitates the creation of custom clients or integrations in virtually any programming language without the need for a dedicated SDK. Developers can directly make HTTP requests using built-in language features or general-purpose HTTP client libraries, such as requests in Python or fetch in JavaScript, to interact with the Gitter API endpoints.