Back to API docs

Go Client Guide

Use the standard library net/http package to call the Shorted API. No external dependencies are required.

Prerequisites

  • Go 1.21+
  • No external dependencies needed

Authentication

Include your API key in the Authorization header as a Bearer token. Public endpoints work without authentication but have lower rate limits.

package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
)

func main() {
	body := bytes.NewBufferString(`{"limit": 10}`)
	req, _ := http.NewRequest("POST",
		"https://shorts-uiekqxovma-km.a.run.app/shorts.v1alpha1.ShortedStocksService/GetTopShorts", body)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer YOUR_API_KEY")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	data, _ := io.ReadAll(resp.Body)
	fmt.Println(string(data))
}

Example: Get Top Shorted Stocks

Retrieve the most heavily shorted stocks on the ASX, sorted by short interest percentage.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

type ShortPosition struct {
	ProductCode    string  `json:"productCode"`
	ProductName    string  `json:"productName"`
	CurrentPercent float64 `json:"currentPercent"`
	Industry       string  `json:"industry"`
	CompanyName    string  `json:"companyName"`
}

type TopShortsResponse struct {
	Stocks     []ShortPosition `json:"stocks"`
	TotalCount int             `json:"totalCount"`
}

func main() {
	body := bytes.NewBufferString(`{"limit": 10, "offset": 0}`)
	resp, err := http.Post(
		"https://shorts-uiekqxovma-km.a.run.app/shorts.v1alpha1.ShortedStocksService/GetTopShorts",
		"application/json", body)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var result TopShortsResponse
	json.NewDecoder(resp.Body).Decode(&result)

	for _, s := range result.Stocks {
		fmt.Printf("%s: %.2f%%\n", s.ProductCode, s.CurrentPercent)
	}
}

Example: Get Stock Details

Fetch detailed information about a specific stock by its ASX code.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

type Stock struct {
	ProductCode    string  `json:"productCode"`
	CompanyName    string  `json:"companyName"`
	CurrentPercent float64 `json:"currentPercent"`
	Industry       string  `json:"industry"`
}

type GetStockResponse struct {
	Stock Stock `json:"stock"`
}

func main() {
	body := bytes.NewBufferString(`{"productCode": "BHP"}`)
	resp, err := http.Post(
		"https://shorts-uiekqxovma-km.a.run.app/shorts.v1alpha1.ShortedStocksService/GetStock",
		"application/json", body)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var result GetStockResponse
	json.NewDecoder(resp.Body).Decode(&result)

	fmt.Printf("%s (%s): %.2f%%\n",
		result.Stock.CompanyName, result.Stock.ProductCode, result.Stock.CurrentPercent)
}

Error Handling

The API returns standard HTTP status codes. Rate limited requests return 429 with a Retry-After header. Always check the response status before parsing the body.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strconv"
	"time"
)

func apiCall(path string, body []byte, apiKey string) ([]byte, error) {
	req, err := http.NewRequest("POST", "https://shorts-uiekqxovma-km.a.run.app"+path, bytes.NewReader(body))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")
	if apiKey != "" {
		req.Header.Set("Authorization", "Bearer "+apiKey)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode == http.StatusTooManyRequests {
		retryAfter, _ := strconv.Atoi(resp.Header.Get("Retry-After"))
		if retryAfter == 0 {
			retryAfter = 60
		}
		fmt.Printf("Rate limited. Retrying in %ds...\n", retryAfter)
		time.Sleep(time.Duration(retryAfter) * time.Second)
		return apiCall(path, body, apiKey)
	}

	if resp.StatusCode != http.StatusOK {
		data, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(data))
	}

	return io.ReadAll(resp.Body)
}

func main() {
	data, err := apiCall(
		"/shorts.v1alpha1.ShortedStocksService/GetTopShorts",
		[]byte(`{"limit": 10}`),
		"YOUR_API_KEY",
	)
	if err != nil {
		panic(err)
	}

	var result map[string]interface{}
	json.Unmarshal(data, &result)
	fmt.Println(result)
}

All Endpoints

See the full API reference for all available endpoints, request/response schemas, and the interactive "Try It" panel.