SDKs overview

Apimetro provides software development kits (SDKs) and libraries to facilitate integration with its public transit data API. These tools encapsulate the underlying HTTP requests and JSON response parsing, allowing developers to focus on application logic rather than network communication details. The official SDKs are maintained by Apimetro and offer comprehensive support for the API's core functionalities, including real-time transit data, historical data access, and service alerts. Community-contributed libraries extend this ecosystem, often providing support for additional programming languages or specialized use cases not covered by the official offerings.

Using an SDK can significantly reduce development time and potential errors by handling authentication, request formatting, and response parsing automatically. For instance, an SDK might manage OAuth 2.0 token refreshes or API key inclusion in headers, as detailed in general API security practices for client-side applications outlined by sources like OAuth.net documentation. Developers can also choose to interact directly with the Apimetro API reference using standard HTTP clients if an SDK for their preferred language is not available or if they require highly customized request handling.

Official SDKs by language

Apimetro maintains official SDKs for popular programming languages, ensuring robust and up-to-date access to its API features. These SDKs are designed to align with the latest API versions and provide idiomatic interfaces for developers. The primary official SDKs focus on languages widely used in web and backend development, reflecting common integration patterns for public transit data applications. Each SDK is typically distributed through its respective language's package manager, simplifying installation and dependency management.

The following table outlines the key official SDKs available:

Language Package Name Install Command Maturity
Python apimetro-python pip install apimetro-python Stable
Node.js @apimetro/node npm install @apimetro/node Stable

These SDKs are continuously updated to reflect new API features and improvements, ensuring developers can always access the latest capabilities. For detailed information on each SDK, including specific class methods and object structures, refer to the Apimetro API documentation.

Installation

Installation of Apimetro's official SDKs follows standard practices for each programming ecosystem. Developers can typically add the library to their project using a single command via their language's package manager. Before installation, it is recommended to ensure that the appropriate language runtime and package manager are installed and configured on your development environment. For example, Python projects often use pip, while Node.js projects rely on npm or yarn.

Python SDK Installation

To install the official Apimetro Python SDK, use pip:

pip install apimetro-python

It is often good practice to install packages within a Python virtual environment to manage project-specific dependencies.

Node.js SDK Installation

For Node.js projects, install the @apimetro/node package using npm:

npm install @apimetro/node

Alternatively, if you are using yarn:

yarn add @apimetro/node

After installation, the SDK can be imported and used within your Python or Node.js application files. API keys, which are required for authentication, should be handled securely, typically through environment variables or a configuration management system, rather than hardcoding them directly into source code, as emphasized in general Google API key best practices.

Quickstart example

This section provides a basic quickstart example demonstrating how to use the Apimetro SDKs to fetch real-time public transit data. The examples show how to initialize the SDK, authenticate with an API key, and make a simple request to retrieve transit information.

Python Quickstart

This Python example fetches a list of active transit lines:

import os
from apimetro_python import ApimetroClient

# Ensure you have your API key set as an environment variable
API_KEY = os.environ.get("APIMETRO_API_KEY")

if not API_KEY:
    raise ValueError("APIMETRO_API_KEY environment variable not set.")

client = ApimetroClient(api_key=API_KEY)

try:
    # Fetch active transit lines
    transit_lines = client.transit.get_lines(status="active")
    print("Active Transit Lines:")
    for line in transit_lines[:5]: # Print first 5 lines
        print(f"- {line.name} ({line.id})")
except Exception as e:
    print(f"An error occurred: {e}")

For more detailed Python SDK usage, consult the Apimetro Python SDK documentation.

Node.js Quickstart

This Node.js example retrieves real-time vehicle positions for a specific transit line:

const { ApimetroClient } = require('@apimetro/node');

// Ensure you have your API key set as an environment variable
const API_KEY = process.env.APIMETRO_API_KEY;

if (!API_KEY) {
  throw new Error('APIMETRO_API_KEY environment variable not set.');
}

const client = new ApimetroClient(API_KEY);

async function getVehiclePositions() {
  try {
    // Replace 'LINE_ID_EXAMPLE' with an actual line ID from your data
    const lineId = 'LINE_ID_EXAMPLE'; 
    const vehicles = await client.realtime.getVehiclePositions(lineId);
    console.log(`Real-time Vehicle Positions for Line ${lineId}:`);
    vehicles.slice(0, 5).forEach(vehicle => { // Log first 5 vehicles
      console.log(`- Vehicle ID: ${vehicle.id}, Lat: ${vehicle.latitude}, Lon: ${vehicle.longitude}`);
    });
  } catch (error) {
    console.error('An error occurred:', error.message);
  }
}

getVehiclePositions();

Refer to the Apimetro Node.js SDK documentation for comprehensive usage patterns.

Community libraries

Beyond the official SDKs, the Apimetro developer community contributes a range of libraries and tools that can enhance integration efforts. These community-driven projects often address specific niche requirements, integrate with particular frameworks, or offer language support that is not yet part of the official SDK roadmap. Examples might include wrappers for Go, Ruby, or PHP, or specialized utilities for data visualization or integration with geographic information systems (GIS).

While community libraries can be valuable, it is important to note that they may not always adhere to the same development and maintenance standards as official SDKs. Developers should review the project's documentation, community activity, and licensing before incorporating them into production systems. Key considerations include:

  • Maintenance Status: Is the library actively maintained and updated to support the latest Apimetro API versions?
  • Documentation: Is the documentation clear, comprehensive, and easy to follow?
  • Community Support: Is there an active community forum, GitHub repository, or other channels for support?
  • Security: Does the library handle API keys and sensitive data securely?

Developers are encouraged to explore community contributions via platforms like GitHub by searching for "Apimetro API" or "Apimetro client" in their preferred programming language. The Apimetro community page may also list notable third-party integrations and libraries.