SDKs overview

Chinese Character Web provides Software Development Kits (SDKs) and client libraries to facilitate interaction with its API. These tools abstract away the complexities of direct HTTP requests, authentication, and response parsing, allowing developers to integrate Chinese dictionary and character data more efficiently into their applications. The available SDKs support common programming languages, offering a structured approach to access functionalities such as Chinese-English dictionary lookups, Pinyin conversion, and stroke order information.

Using an SDK can streamline development by providing idiomatic language constructs and handling underlying API communication protocols. This approach is consistent with common API integration practices, as outlined by organizations like the World Wide Web Consortium's Web of Things, which emphasize standardized methods for device and service interaction. Chinese Character Web's SDKs are designed to align with these principles, enabling developers to focus on application logic rather than API mechanics.

Official SDKs by language

Chinese Character Web offers official SDKs for several programming languages, designed to provide direct access to the API's features. These SDKs are maintained by Chinese Character Web to ensure compatibility and optimal performance with the latest API versions. The table below outlines the primary official SDKs, their respective package names, installation methods, and general maturity status.

Language Package Name Installation Command Maturity
Python chinesecharacterweb-py pip install chinesecharacterweb-py Stable
JavaScript @chinesecharacterweb/client npm install @chinesecharacterweb/client or yarn add @chinesecharacterweb/client Stable
PHP chinesecharacterweb/php-sdk composer require chinesecharacterweb/php-sdk Stable

Installation

Installation of the Chinese Character Web SDKs follows standard package management practices for each respective language. Prior to installation, developers should ensure they have the appropriate package manager (e.g., pip for Python, npm/yarn for JavaScript, Composer for PHP) configured in their development environment. Detailed installation instructions and system requirements are available in the Chinese Character Web API documentation.

Python SDK Installation

To install the Python SDK, use pip, Python's package installer. This command retrieves the latest stable version of the library from the Python Package Index (PyPI).

pip install chinesecharacterweb-py

For development environments, it is often recommended to install packages within a Python virtual environment to manage dependencies effectively.

JavaScript SDK Installation

The JavaScript SDK can be installed using npm (Node Package Manager) or Yarn. These package managers are commonly used in Node.js and front-end JavaScript projects.

Using npm:

npm install @chinesecharacterweb/client

Using Yarn:

yarn add @chinesecharacterweb/client

This will add the @chinesecharacterweb/client package to your project's node_modules directory and update your package.json file.

PHP SDK Installation

For PHP projects, Composer is the standard dependency manager. The PHP SDK can be installed by running the following command in your project's root directory:

composer require chinesecharacterweb/php-sdk

This command will download the SDK and its dependencies, and Composer will generate an autoloader file (vendor/autoload.php) that you can include in your project.

Quickstart example

The following quickstart examples demonstrate basic usage of the Chinese Character Web SDKs to perform a dictionary lookup. These snippets illustrate the typical workflow: initializing the client with an API key, making a request, and processing the response. For more complex operations or error handling, refer to the full Chinese Character Web API documentation.

Python Quickstart Example

This Python example demonstrates how to use the chinesecharacterweb-py SDK to look up a Chinese character.


import os
from chinesecharacterweb import Client

# Ensure you set your API key as an environment variable or replace 'YOUR_API_KEY'
api_key = os.environ.get('CHINESE_CHARACTER_WEB_API_KEY', 'YOUR_API_KEY')
client = Client(api_key=api_key)

try:
    # Example: Look up the character '你好'
    result = client.dictionary.lookup(character='你好')
    print(f"Character: {result['character']}")
    print(f"Pinyin: {result['pinyin']}")
    print(f"Definitions: {', '.join(result['definitions'])}")
except Exception as e:
    print(f"An error occurred: {e}")

JavaScript Quickstart Example (Node.js)

This Node.js example uses the @chinesecharacterweb/client SDK to perform a dictionary lookup.


const { ChineseCharacterWebClient } = require('@chinesecharacterweb/client');

// Ensure you set your API key as an environment variable or replace 'YOUR_API_KEY'
const apiKey = process.env.CHINESE_CHARACTER_WEB_API_KEY || 'YOUR_API_KEY';
const client = new ChineseCharacterWebClient(apiKey);

async function lookupCharacter() {
  try {
    // Example: Look up the character '学习'
    const result = await client.dictionary.lookup('学习');
    console.log(`Character: ${result.character}`);
    console.log(`Pinyin: ${result.pinyin}`);
    console.log(`Definitions: ${result.definitions.join(', ')}`);
  } catch (error) {
    console.error('An error occurred:', error.message);
  }
}

lookupCharacter();

PHP Quickstart Example

This PHP example illustrates how to use the chinesecharacterweb/php-sdk to perform a dictionary lookup.


<?php
require_once __DIR__ . '/vendor/autoload.php';

use ChineseCharacterWeb\Client;

// Ensure you set your API key as an environment variable or replace 'YOUR_API_KEY'
$apiKey = getenv('CHINESE_CHARACTER_WEB_API_KEY') ?: 'YOUR_API_KEY';
$client = new Client($apiKey);

try {
    // Example: Look up the character '朋友'
    $result = $client->dictionary->lookup('朋友');
    echo "Character: " . $result['character'] . "\n";
    echo "Pinyin: " . $result['pinyin'] . "\n";
    echo "Definitions: " . implode(', ', $result['definitions']) . "\n";
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage() . "\n";
}
?>

Community libraries

Beyond the official SDKs, the Chinese Character Web ecosystem benefits from community-contributed libraries and wrappers. These libraries are developed and maintained independently by developers and may offer alternative features, different language support, or specialized integrations. While official SDKs are typically recommended for stability and direct support, community libraries can provide valuable solutions for specific use cases or preferred programming environments.

Developers interested in community contributions should consult public code repositories like GitHub or specialized package registries (e.g., PyPI, npmjs.com) by searching for "Chinese Character Web" or related terms. It is important to note that community libraries may vary in terms of maintenance, documentation quality, and compatibility with the latest API versions. For instance, projects like HanziJS offer client-side JavaScript libraries for Chinese character data, which might complement or serve as an alternative to direct API calls depending on the application's needs.

When considering a community library, it is advisable to review its activity, open issues, and contribution guidelines to assess its reliability and ongoing support. The official Chinese Character Web documentation remains the authoritative source for API specifications, regardless of the client library used.