SDKs overview
Code::Stats offers a set of Software Development Kits (SDKs) and libraries designed to facilitate the programmatic submission of coding activity data to its service. While Code::Stats primarily integrates through editor plugins for various IDEs and text editors, these SDKs provide an alternative for developers who need to track activity from custom scripts, unsupported environments, or specialized tools. The SDKs abstract the direct interaction with the Code::Stats API endpoints, simplifying authentication and data formatting for keystroke and file event submissions.
The core function of these SDKs is to send pulse events, which are the fundamental unit of activity tracking in Code::Stats. A pulse typically represents a short period of active coding, often triggered by keystrokes or file modifications. Developers can use these SDKs to implement custom activity trackers, extend existing editor integrations, or integrate Code::Stats into build processes or CI/CD pipelines to monitor developer engagement. The availability of SDKs across popular languages supports a broad range of development ecosystems, ensuring flexibility in how users choose to submit their coding statistics.
Official SDKs by language
Code::Stats provides official SDKs for several programming languages, maintained by the project team. These SDKs are designed to offer a consistent and reliable way to interact with the Code::Stats API. Each SDK typically handles API authentication, request formatting, and error handling, allowing developers to focus on integrating activity tracking into their applications or scripts rather than managing the low-level API communication. The following table outlines the officially supported SDKs, their typical package names, and installation commands.
| Language | Package/Module Name | Installation Command | Maturity |
|---|---|---|---|
| Python | codestats.py (script) |
pip install requests (for dependencies) |
Stable |
| Go | github.com/CodeStats/codestats-go |
go get github.com/CodeStats/codestats-go |
Stable |
| JavaScript | codestats-cli (npm package) |
npm install -g codestats-cli |
Stable |
| Ruby | codestats.rb (script) |
gem install rest-client (for dependencies) |
Stable |
| PHP | codestats.php (script) |
Composer (no dedicated package, script usage) | Stable |
| Rust | codestats-cli (crate) |
cargo install codestats-cli |
Stable |
For detailed usage and specific API methods available within each SDK, developers should consult the Code::Stats API documentation directly. This resource provides information on endpoint specifications, data structures for pulses, and authentication requirements, which are critical for successful integration. Each SDK typically mirrors these API capabilities in a language-idiomatic way.
Installation
Installing Code::Stats SDKs and related tools generally follows standard package management practices for each respective language. The process involves using the language's package manager to fetch and install the necessary libraries or utility scripts. Below are common installation instructions for the officially supported languages.
Python
The Python integration often involves a script rather than a formal package. You might need to install the requests library for HTTP communication if you are not relying on a standalone script that bundles its dependencies. To install requests:
pip install requests
You would then download the codestats.py script from the official Code::Stats Python repository and execute it as needed to send pulses.
Go
For Go, the SDK is available via go get:
go get github.com/CodeStats/codestats-go
After installation, you can import the package into your Go project and use its functions to send activity data. The Go SDK typically provides functions for creating and sending pulse events, handling API keys, and managing the connection to the Code::Stats service.
JavaScript (Node.js)
The JavaScript client is often distributed as a command-line interface (CLI) tool via npm:
npm install -g codestats-cli
This global installation allows you to run the codestats command from your terminal to send pulses. If integrating into a Node.js project, you might install it as a local dependency: npm install codestats-cli and then import its modules.
Ruby
Similar to Python, Ruby often uses a direct script approach. You may need the rest-client gem:
gem install rest-client
The codestats.rb script can then be downloaded from the Code::Stats Ruby repository and executed. This script typically includes logic to read your API key and send formatted data.
PHP
PHP integration often involves a standalone script. While there isn't a dedicated Composer package for a full SDK, you would typically use PHP's built-in HTTP client functions or a library like Guzzle (installed via Composer) to make API requests. For example, if using Guzzle:
composer require guzzlehttp/guzzle
Then, you would implement the logic to construct and send pulse requests as described in the Code::Stats API documentation using Guzzle or similar HTTP client libraries.
Rust
The Rust client is available as a crate through Cargo:
cargo install codestats-cli
This command installs the codestats-cli binary, which can be run from the command line. For programmatic use within a Rust project, you would add codestats-cli as a dependency in your Cargo.toml and then use its provided functions.
Quickstart example
This quickstart example demonstrates how to send a simple pulse event using the Python SDK (or a direct script approach) to Code::Stats. This example assumes you have your Code::Stats API key and a machine ID configured. These are essential for authenticating your requests and associating activity with your profile. Your API key can be found on your Code::Stats account page.
Python Quickstart
First, ensure you have the requests library installed:
pip install requests
Next, create a Python script (e.g., send_pulse.py) with the following content. Replace YOUR_API_KEY and YOUR_MACHINE_ID with your actual credentials.
import requests
import json
import time
API_KEY = "YOUR_API_KEY" # Replace with your actual Code::Stats API key
MACHINE_ID = "YOUR_MACHINE_ID" # Replace with your actual Code::Stats machine ID
def send_pulse(language, xp_amount):
url = "https://codestats.net/api/pulses"
headers = {
"X-API-Token": API_KEY,
"X-Machine-ID": MACHINE_ID,
"Content-Type": "application/json"
}
payload = {
"time": int(time.time()),
"coded_in": {
language: xp_amount
}
}
try:
response = requests.post(url, headers=headers, data=json.dumps(payload))
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
print(f"Pulse sent successfully for {language} with {xp_amount} XP.")
print(f"Response: {response.json()}")
except requests.exceptions.RequestException as e:
print(f"Error sending pulse: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f"Response content: {e.response.text}")
if __name__ == "__main__":
# Example: Send a pulse for Python with 10 XP
send_pulse("Python", 10)
# Example: Send another pulse for JavaScript with 5 XP
send_pulse("JavaScript", 5)
To run this example, save the script and execute it from your terminal:
python send_pulse.py
This script defines a send_pulse function that constructs a JSON payload containing the current timestamp and the experience points (XP) gained for a specific language. It then sends this payload as a POST request to the Code::Stats /api/pulses endpoint, including the API token and machine ID in the headers for authentication. A successful response indicates that the activity has been recorded on your Code::Stats profile. For more advanced usage, such as tracking multiple languages in a single pulse or handling file events, refer to the Code::Stats API specification.
Community libraries
Beyond the official SDKs, the Code::Stats ecosystem benefits from community-contributed libraries and integrations. These unofficial tools often extend support to additional programming languages, integrate with niche editors, or provide specialized features not covered by the official offerings. While not officially maintained by the Code::Stats team, these community projects can offer valuable alternatives for developers working in specific environments.
Community libraries typically emerge from developers seeking to integrate Code::Stats into their preferred tools or languages where an official SDK might not exist or might not fully meet their specific requirements. These can include:
- Editor-specific plugins: Integrations for less common text editors or IDEs that do not have direct official support.
- Language-specific clients: Libraries for languages like C#, Java, or Swift, allowing developers to build custom trackers in these environments.
- Utility scripts: Small scripts designed for specific tasks, such as parsing log files and sending bulk pulses, or integrating with project management tools.
- Framework integrations: Libraries that make it easier to embed Code::Stats tracking into web frameworks or desktop applications.
When considering a community library, it is advisable to check its active maintenance status, documentation, and community support. Resources like GitHub often host these projects, allowing users to review source code, report issues, and contribute. While the official documentation focuses on the core API and official SDKs, exploring community repositories can reveal a broader range of integration possibilities. For example, a developer might find community-driven JavaScript libraries that offer more granular control over pulse events than the official CLI, or a C# library for integrating Code::Stats into a .NET application. Always verify the security and reliability of third-party libraries before integrating them into production environments.