Getting started overview

To begin interacting with the Gitter API, developers need to establish an account, register an application to obtain client credentials, and then use OAuth2 to acquire an access token. This token authenticates subsequent API requests, allowing programmatic access to Gitter's chat rooms, messages, and user information. The API supports standard HTTP methods (GET, POST, PUT, DELETE) and typically returns data in JSON format. The Gitter API is designed for integrating Gitter's communication features into external applications or for automating tasks within Gitter communities.

The core process involves:

  1. Account Creation: Registering on Gitter.im.
  2. Application Registration: Creating a new OAuth application to obtain a Client ID and Client Secret.
  3. Authorization Flow: Directing users to Gitter for authorization and receiving an OAuth2 access token.
  4. API Calls: Using the access token to make authenticated requests to Gitter API endpoints.

This guide focuses on the steps required to make a first successful API call, providing the foundational knowledge for further development.

Create an account and get keys

Accessing the Gitter API requires a Gitter user account and an OAuth2 access token. The process involves registering an application within Gitter to obtain the necessary client credentials.

Step 1: Create a Gitter account

If you do not already have one, create a Gitter account by visiting the Gitter login page and choosing a registration method (GitHub, GitLab, or Twitter). Gitter primarily integrates with these platforms for user authentication.

Step 2: Register your application

After logging in, navigate to the Gitter Developer Applications page. Here, you can register a new OAuth application. This step is crucial for obtaining your Client ID and Client Secret, which are required for the OAuth2 authorization flow.

When registering your application, you will need to provide:

  • Application Name: A descriptive name for your application.
  • Homepage URL: The URL where your application is hosted.
  • Callback URL: The URL to which Gitter will redirect the user after they authorize your application. This URL must be precise as it is used in the OAuth2 flow. For local development, http://localhost:3000/callback (or a similar port) is commonly used.

Upon successful registration, Gitter will provide you with a Client ID and a Client Secret. Treat your Client Secret as sensitive information and protect it from unauthorized access, as advised by general OAuth 2.0 client registration best practices.

Step 3: Obtain an OAuth2 access token

Gitter utilizes the OAuth2 Authorization Code grant type for obtaining access tokens. This flow involves redirecting a user to Gitter for authorization, and Gitter then redirects the user back to your specified Callback URL with an authorization code.

The general steps are:

  1. Redirect for Authorization: Construct a URL to Gitter's authorization endpoint, including your Client ID, Callback URL, and requested scope (e.g., read write).
  2. User Authorization: The user grants your application permission.
  3. Callback with Code: Gitter redirects the user to your Callback URL with an authorization code in the query parameters.
  4. Exchange Code for Token: Your application makes a server-side POST request to Gitter's token endpoint, exchanging the code for an access_token. This request must include your Client ID and Client Secret.

For a quick start or testing, you can generate a personal access token directly from your Gitter Developer Applications page. While convenient for initial testing, personal access tokens are generally not recommended for production applications where user-specific authorization is required. For this guide, we will use a personal access token for simplicity.

Your first request

Once you have obtained an access token, you can make authenticated requests to the Gitter API. We will demonstrate how to fetch information about the current user, a common first step to verify authentication.

Prerequisites

  • A Gitter personal access token (from Gitter Developer Applications).
  • A tool for making HTTP requests, such as curl or a programming language's HTTP client.

API Endpoint: Get current user

The endpoint to retrieve information about the authenticated user is /v1/user, as documented in the Gitter API reference for current user.

Example Request (using curl)

Replace YOUR_ACCESS_TOKEN with your actual personal access token.


curl -X GET \
  -H "Accept: application/json" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  "https://api.gitter.im/v1/user"

Expected Response

A successful response will return a JSON array containing a single user object, representing the authenticated user. The structure will resemble:


[
  {
    "id": "[user_id]",
    "username": "yourusername",
    "displayName": "Your Display Name",
    "url": "/yourusername",
    "avatarUrlSmall": "https://avatars.githubusercontent.com/...",
    "avatarUrlMedium": "https://avatars.githubusercontent.com/...",
    "v": 1,
    "gv": "[gv_value]",
    "li": "[li_value]"
  }
]

This response confirms that your access token is valid and correctly configured for API access.

Quick Reference: Getting Started Steps

Step What to Do Where
1. Create Account Register on Gitter.im Gitter Login Page
2. Register App Create new OAuth application to get Client ID/Secret Gitter Developer Applications
3. Get Access Token Generate a personal access token for testing Gitter Developer Applications
4. Make Request Use curl or HTTP client with access token https://api.gitter.im/v1/user

Common next steps

After successfully making your first API call, consider these next steps to further integrate with the Gitter API:

Interacting with Rooms

Gitter's primary function revolves around chat rooms. You can use the API to:

  • List rooms: Retrieve a list of rooms the authenticated user is part of or can access. The Gitter Rooms API reference provides details on endpoints like /v1/rooms.
  • Join/Leave rooms: Programmatically manage user participation in rooms.
  • Get room details: Fetch specific information about a room, such as its ID, name, and topic.

Sending and Receiving Messages

The ability to send and receive messages is central to building interactive applications:

  • Post messages: Send messages to specific rooms using the /v1/rooms/:roomId/chatMessages endpoint, as described in the Gitter Messages API documentation.
  • Fetch message history: Retrieve past messages from a room.
  • Real-time updates: Implement real-time message updates using Gitter's Streaming API, which leverages Server-Sent Events (SSE) for continuous data flow.

User Management and Webhooks

Beyond basic chat, the Gitter API offers features for user interaction and event notification:

  • User resources: Access information about other users within Gitter.
  • Webhooks: Set up webhooks to receive notifications in your application when specific events occur in Gitter, such as new messages or users joining a room. The Gitter Webhooks documentation details how to configure these.

OAuth2 Implementation for Production

For production applications, it is essential to implement the full OAuth2 authorization code flow rather than relying solely on personal access tokens. This ensures that your application requests only the necessary permissions and that user data is handled securely. Refer to the Gitter Authentication documentation for a comprehensive guide on implementing OAuth2.

Troubleshooting the first call

When making your initial API call, several common issues can arise. Here are some troubleshooting tips:

401 Unauthorized

This is the most frequent error and indicates an issue with your authentication. Check the following:

  • Access Token Validity: Ensure your personal access token has not expired or been revoked. Generate a new one from your Gitter Developer Applications page if in doubt.
  • Bearer Token Format: Verify that the Authorization header is correctly formatted as Bearer YOUR_ACCESS_TOKEN, with a space between "Bearer" and your token.
  • Scope: Ensure the access token has the necessary permissions (scope) for the endpoint you are trying to access. For fetching user details, read scope is typically sufficient.

403 Forbidden

A 403 error means your token is valid, but it lacks the permissions to access the requested resource. This often happens if:

  • Insufficient Scope: Your access token might not have the required write scope for posting messages or other actions.
  • Resource Access: You might be trying to access a private room or user information that your authenticated user does not have permission to view.

404 Not Found

This error indicates that the requested API endpoint or resource does not exist:

  • Endpoint URL: Double-check the API endpoint URL for typos. Ensure you are using https://api.gitter.im as the base URL.
  • Resource ID: If you are accessing a specific room or message, verify that the :roomId or :messageId in your URL is correct and exists.

Network Issues or SSL Errors

If you receive errors related to network connectivity or SSL/TLS, ensure your environment can reach https://api.gitter.im and that your HTTP client is configured to handle SSL certificates correctly.

JSON Parsing Errors

If the API returns a response but your application struggles to parse it, confirm that you are:

  • Setting the Accept: application/json header in your request.
  • Using a robust JSON parser in your programming language to handle the response body.

Consult the Gitter API documentation for specific error codes and their meanings, which can provide more targeted troubleshooting guidance.