Deploy AKTools with Docker and Build a Gin API for Real‑Time Stock Data

This guide shows how to quickly set up AKTools via Docker, retrieve high‑quality historical and real‑time stock data, upgrade AKShare, and create a flexible Gin‑based API to serve the data for quantitative analysis or stock‑screening applications.

Code Wrench
Code Wrench
Code Wrench
Deploy AKTools with Docker and Build a Gin API for Real‑Time Stock Data

Why use AKTools

AKTools is built on the actively maintained AKShare library and provides free, comprehensive financial data interfaces such as historical stock prices, real‑time quotes, and industry information. Deploying it with Docker gives a reproducible environment without manual dependency handling.

Docker deployment of AKTools

Pull and run the image

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

runs the container in the background. -p 18080:8080 maps the container’s internal port 8080 to host port 18080.

Verify the service

After the container starts, open http://127.0.0.1:18080 in a browser to confirm the service is reachable.

Test the AKTools API

Request historical data for a sample stock (e.g., 600066) to ensure the API works:

http://127.0.0.1:18080/api/public/stock_zh_a_hist?symbol=600066

A JSON response containing the stock’s historical records indicates a successful deployment.

AKTools API test result
AKTools API test result

Upgrade AKShare inside the container

Enter the running AKTools container and upgrade AKShare to the latest version:

docker run -it ak_tools:latest /bin/bash
pip install akshare --upgrade -i https://pypi.org/simple

This ensures access to newly added endpoints and data improvements.

Wrap AKTools with a Gin handler

The following Go code demonstrates a Gin handler that forwards a stock‑symbol query to AKTools and returns the raw JSON response:

// GetStocksHandler retrieves historical stock data
func GetStocksHandler(c *gin.Context) {
    // Get the stock symbol from query parameters
    symbol := c.Query("symbol")
    if symbol == "" {
        c.JSON(http.StatusBadRequest, gin.H{"error": "symbol is required"})
        return
    }
    // Build the request URL
    url := fmt.Sprintf("http://127.0.0.1:18080/api/public/stock_zh_a_hist?symbol=%s", symbol)
    // Send HTTP request
    resp, err := http.Get(url)
    if err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to connect to data source: " + err.Error()})
        return
    }
    defer resp.Body.Close()
    if resp.StatusCode != http.StatusOK {
        c.JSON(resp.StatusCode, gin.H{"error": fmt.Sprintf("Failed to fetch data, status code: %d", resp.StatusCode)})
        return
    }
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read data: " + err.Error()})
        return
    }
    // Return the raw JSON data
    c.Data(http.StatusOK, "application/json", body)
}

Source code repositories

GitHub https://github.com/louis-xie-programmer/go-stock-analyzer
Gitee (China mirror) https://gitee.com/louis_xie/go-stock-analyzer
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerGoAPIStock DataAKTools
Code Wrench
Written by

Code Wrench

Focuses on code debugging, performance optimization, and real-world engineering, sharing efficient development tips and pitfall guides. We break down technical challenges in a down-to-earth style, helping you craft handy tools so every line of code becomes a problem‑solving weapon. 🔧💻

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.