SDKs overview

Open Collective provides developers with tools to interact programmatically with its platform, primarily through a GraphQL API. This API allows for querying data related to collectives, contributors, expenses, and transactions, as well as initiating certain actions. Software Development Kits (SDKs) and client libraries simplify this interaction by abstracting HTTP requests, handling authentication, and providing language-specific interfaces.

The platform is designed to support transparent financial management for open-source projects, community groups, and other initiatives. Developers can use the API and associated SDKs to build custom dashboards, automate reporting, integrate with other services, or extend the functionality of the Open Collective platform itself. Authentication for API access is managed using Personal Access Tokens (PATs), which can be generated and managed through a user's Open Collective account settings. These tokens require specific scopes to perform authorized actions, adhering to the principle of least privilege.

While Open Collective maintains official JavaScript client libraries, the GraphQL API structure also encourages the development of community-contributed libraries in various programming languages. This approach allows developers to choose the tools best suited for their specific tech stacks. The GraphQL nature of the API facilitates client-side querying efficiency, enabling fetching only the required data with a single request, which can reduce over-fetching compared to traditional REST APIs, as described by GraphQL.org documentation on queries.

Official SDKs by language

Open Collective primarily supports JavaScript for its official client libraries, catering to both Node.js environments and browser-based applications. These libraries are designed to provide a structured way to interact with the Open Collective GraphQL API, simplifying data fetching and mutation operations. The official offerings focus on robust and well-maintained interfaces that align with the platform's core functionalities.

The main official library is @opencollective/sdk, which serves as a general-purpose client for the Open Collective API. It facilitates common operations such as retrieving collective information, managing contributions, and interacting with expense data. This library is regularly updated to reflect changes and enhancements in the underlying GraphQL API.

Language Package Name Primary Use Maturity
JavaScript (Node.js/Browser) @opencollective/sdk General API interaction, data fetching, mutations Stable
TypeScript (Type Definitions) Included with @opencollective/sdk Enhanced developer experience with type safety Stable

Installation

Installing the official Open Collective SDK typically involves using a package manager like npm or yarn for JavaScript projects. The process is straightforward and integrates into standard front-end or back-end JavaScript development workflows. Prior to installation, ensure that Node.js and a package manager are installed on your system.

Node.js and Browser Environments

To install the main Open Collective SDK package, use one of the following commands in your project directory:

Using npm:

npm install @opencollective/sdk

Using yarn:

yarn add @opencollective/sdk

After installation, the SDK can be imported into your JavaScript or TypeScript files. The package includes TypeScript definitions, providing type-checking and improved autocompletion for developers working with TypeScript.

For browser-based applications, the installed package can be bundled using tools like Webpack or Rollup, or directly imported if your environment supports ES Modules. The SDK is designed to be compatible with modern browser environments, allowing for client-side API interactions, subject to appropriate security considerations like not exposing Personal Access Tokens directly in client-side code.

Quickstart example

This quickstart demonstrates how to fetch information for a specific collective using the @opencollective/sdk in a Node.js environment. This example assumes you have installed the SDK and have an Open Collective Personal Access Token with the necessary permissions.

First, create a new JavaScript file (e.g., getCollective.js) and add the following code:

const { OpenCollectiveSDK } = require('@opencollective/sdk');

// Replace with your actual Personal Access Token
const PERSONAL_ACCESS_TOKEN = 'YOUR_OPENCOLLECTIVE_PERSONAL_ACCESS_TOKEN';

// Replace with the slug of the collective you want to fetch
const COLLECTIVE_SLUG = 'opencollective'; 

const sdk = new OpenCollectiveSDK({ 
  token: PERSONAL_ACCESS_TOKEN 
});

async function getCollectiveData() {
  try {
    const collective = await sdk.Collective.getOne(COLLECTIVE_SLUG);
    console.log('Collective Name:', collective.name);
    console.log('Collective Description:', collective.description);
    console.log('Collective Website:', collective.website);
    console.log('Collective Balance (USD):', collective.stats.balance.value / 100); // Balance is in cents
  } catch (error) {
    console.error('Error fetching collective data:', error.message);
  }
}

getCollectiveData();

Steps to run this example:

  1. Install the SDK: If you haven't already, run npm install @opencollective/sdk or yarn add @opencollective/sdk in your project directory.
  2. Obtain a Personal Access Token: Go to your Open Collective profile settings, navigate to "Developer" or "API Keys", and generate a new Personal Access Token. Ensure it has at least read access to collectives.
  3. Replace placeholders: Update YOUR_OPENCOLLECTIVE_PERSONAL_ACCESS_TOKEN with your actual token and opencollective with the slug of the collective you wish to query (e.g., webpack, babel, or any other public collective).
  4. Execute the script: Run node getCollective.js from your terminal.

This script will connect to the Open Collective API, retrieve the specified collective's data, and print its name, description, website, and current balance to the console. The balance is typically returned in cents, so the example divides by 100 to display it in dollars.

Quickstart for creating a contribution

While the official SDK focuses on data retrieval, creating contributions or managing expenses often involves more complex workflows, sometimes requiring user interaction for payment processing. Direct creation of financial contributions via an SDK typically requires specific permissions and adherence to payment processor flows. For direct financial transactions, integrating with Open Collective's payment processors or using their embedded contribution forms is generally recommended for security and compliance reasons.

Community libraries

Beyond the official JavaScript SDK, the Open Collective community has developed client libraries and tools in various other programming languages to facilitate interaction with the GraphQL API. These community-driven efforts extend the reach of Open Collective's API to broader developer ecosystems, offering alternatives for projects not based on JavaScript.

These libraries vary in maturity, maintenance status, and the extent of API coverage. Developers considering using a community library should review its documentation, GitHub repository activity, and contribution guidelines to assess its suitability for their project. Common uses for these libraries include:

  • Data synchronization: Pulling collective data into local databases or other applications.
  • Reporting and analytics: Generating custom reports based on collective financial data.
  • Automated tasks: Scripting interactions with collectives for administrative or informational purposes.

Examples of community-supported languages often include:

  • Python: Python is frequently used for scripting and data processing, making it a common choice for community API clients. Developers might find libraries that wrap GraphQL queries into Pythonic objects or functions.
  • Ruby: For Ruby-on-Rails applications or other Ruby projects, community gems may exist to simplify API calls, aligning with Ruby's convention-over-configuration philosophy.
  • PHP: Websites and applications built with PHP frameworks like Laravel or Symfony could utilize community-developed PHP clients to integrate Open Collective data.

To discover the latest community libraries, developers are encouraged to search public code repositories like GitHub for "Open Collective API client" or "Open Collective SDK" in their preferred language. The Open Collective documentation sometimes links to these community resources, but their existence and ongoing support may be subject to community efforts rather than official endorsement.