AKTools Documentation

repository·main·Indexed 23 days ago

https://github.com/akfamily/aktools

AKTools is a FastAPI-based HTTP API wrapper for the AKShare financial data library. It allows developers using non-Python languages—such as Java, Go, JavaScript, Rust, R, and MATLAB—to consume AKShare's financial data through standard HTTP requests. The tool includes a CLI for server management, Docker deployment options, and provides interactive OpenAPI/Swagger documentation.

Tokens
3.6K
Snippets
14
Records
22
Agent score
79%

What's inside AKTools

  1. Access AKShare data via HTTP API

    main

    Once the server is running, you can access AKShare's financial data interfaces via HTTP requests. The API follows a specific URL pattern:

    • No-parameter interfaces: http://<host>:<port>/api/public/<interface_name>
    • Parameter-based interfaces: http://<host>:<port>/api/public/<interface_name>?param1=value1&param2=value2

    Note: When passing parameters in the URL, do not include quotes around the values (e.g., use symbol=600000, not symbol="600000").

  2. Upgrade AKShare inside a Docker container

    main

    While pulling a new image usually installs the latest AKShare version, you can manually upgrade AKShare within an existing container and commit the changes to a new image.

    1. Tag the existing image:
      docker tag registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95 ak_tools:1.8.95
    2. Run the container in interactive mode:
      docker run -it ak_tools:1.8.95 /bin/bash
    3. Upgrade AKShare:
      pip install akshare --upgrade -i https://pypi.org/simple
    4. Exit the container:
      exit
    5. Find the Container ID:
      docker ps -a
    6. Commit the changes to a new image:
      # Replace <container_id> with the ID from step 5
      docker commit -m "update akshare to latest" <container_id> ak_tools:1.8.96
    7. Start the new container:
      docker run -p 8080:8080 ak_tools:1.8.96
    docker commit -m "update akshare to latest" a07c8632637f ak_tools:1.8.96
  3. Build AKTools image locally

    main

    If you want to build the image from source, use the Dockerfile provided in the repository.

    1. Download the Dockerfile from the repository.
    2. Build the image:
      docker build -t aktools:v1 .
    3. Run the local image:
      • Foreground: docker run -p 8080:8080 aktools:v1
      • Background: docker run -d -p 8080:8080 aktools:v1

    Verify the build by accessing http://127.0.0.1:8080/api/public/stock_zh_a_hist.

    docker build -t aktools:v1 .
    docker run -p 8080:8080 aktools:v1
  4. Install and run AKTools via Docker

    main

    You can deploy AKTools using pre-built Docker images from the Aliyun registry. The image version corresponds to the AKShare version.

    1. Download the image

    Use docker pull with the specific version tag:

    docker pull registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95

    2. Run the image

    AKTools supports two modes of operation:

    High Performance Mode (Default)

    This mode uses Gunicorn as the WSGI server to improve network processing performance.

    Run in foreground:

    docker run -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95

    Run in background (detached):

    docker run -d -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95

    Standard Mode

    Runs the application directly using the aktools module.

    Run in foreground:

    docker run -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95 python -m aktools --host 0.0.0.0 --port 8080

    Run in background (detached):

    docker run -d -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95 python -m aktools --host 0.0.0.0 --port 8080

    3. Verify the installation

    Once running, access the API via your browser or a tool like curl at: http://127.0.0.1:8080/api/public/stock_zh_a_hist

  5. Access AKTools API and Homepage

    main

    Once the service is running, you can interact with it via a web browser or HTTP client:

    • Homepage: Visit http://127.0.0.1:8080/ to see the AKTools homepage and reference information.
    • Test Data API: Visit http://127.0.0.1:8080/api/public/stock_zh_a_hist to test a public stock history endpoint.
    • API with Parameters: Pass parameters via query strings, for example: http://127.0.0.1:8080/api/public/stock_zh_a_hist?symbol=600000.
  6. Install AKTools via pip

    main

    Install the aktools package using pip. AKTools provides an HTTP API wrapper for AKShare, allowing non-Python languages (like C++, Java, Go, Rust, JavaScript, R, etc.) to access financial data via HTTP requests.

    pip install aktools
  7. Access the AKTools API routes

    main

    The AKTools application organizes its functionality into several router prefixes:

    • /api: Contains the core data interfaces (powered by app_core).
    • /auth: Contains authentication and login interfaces (powered by app_user_login).
    • /: The root path serves the homepage via a Jinja2 template.
  8. Run the AKTools FastAPI server

    main

    AKTools is a FastAPI-based application designed to deploy AKShare data interfaces as HTTP APIs. You can run the server locally using uvicorn. By default, the application is configured to run on 127.0.0.1 at port 8080 when executed as a script.

    if __name__ == "__main__":
        uvicorn.run(app="main:app", host="127.0.0.1", port=8080)