API

tokei-api provides a consistent RESTful API for analyzing code statistics in Git repositories.

Core API

  • POST /api/analyses - Analyze a repository
  • GET /api/analyses?url=... - Retrieve cached analysis result by repository URL
  • GET /api/analyses/:id - Retrieve a specific analysis result
  • GET /api/analyses/:id/languages - Get language statistics
  • GET /api/analyses/:id/badges/:type - Get badge data
  • GET /api/badge/:type?url=... - Retrieves badge data in Shields.io compatible format for the specified repository. Available badge types: lines, language, languages, ratio.

GitHub-specific API

  • GET /api/github/:owner/:repo - Analyze a GitHub repository
  • GET /api/github/:owner/:repo/languages - Get language statistics for a GitHub repository
  • GET /api/github/:owner/:repo/badges/:type - Get badge data for a GitHub repository

Badge API

  • GET /badge/github/:owner/:repo/:type - Simplified URL for retrieving badge data in shields.io compatible format

Available badge types: lines, language, languages, ratio

Usage Examples


// Analyze a repository
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"url":"https://github.com/kojix2/tokei-api"}' \
  https://tokei.kojix2.net/api/analyses

// Retrieve cached analysis result
curl "https://tokei.kojix2.net/api/analyses?url=https://github.com/kojix2/tokei-api"

// Analyze a GitHub repository directly
curl https://tokei.kojix2.net/api/github/kojix2/tokei-api

// Retrieve badge data for any Git repository
curl "https://tokei.kojix2.net/api/badge/lines?url=https://github.com/kojix2/tokei-api"
        

Response Format

All API endpoints return JSON responses. Here's an example response from GET /api/github/:owner/:repo:

{
  "data": {
    "id": "12345678-1234-1234-1234-123456789012",
    "url": "https://github.com/kojix2/tokei-api",
    "owner": "kojix2",
    "repo": "tokei-api",
    "analyzed_at": "2023-01-01T12:00:00Z",
    "status": "completed",
    "summary": {
      "total_lines": 5000,
      "total_code": 4000,
      "total_comments": 500,
      "total_blanks": 500,
      "languages_count": 5,
      "top_language": "Crystal",
      "code_comment_ratio": 8.0
    },
    "languages": {
      "Crystal": {
        "files": 10,
        "code": 3000,
        "comments": 400,
        "blanks": 300
      },
      "JavaScript": {
        "files": 2,
        "code": 500,
        "comments": 50,
        "blanks": 100
      },
      "...": {}
    },
    "links": {
      "self": "/api/github/kojix2/tokei-api",
      "languages": "/api/github/kojix2/tokei-api/languages",
      "web": "/github/kojix2/tokei-api",
      "badges": {
        "lines": "/api/github/kojix2/tokei-api/badges/lines",
        "language": "/api/github/kojix2/tokei-api/badges/language",
        "languages": "/api/github/kojix2/tokei-api/badges/languages",
        "ratio": "/api/github/kojix2/tokei-api/badges/ratio"
      }
    }
  }
}

Error Handling

In case of an error, the API returns an appropriate HTTP status code along with a JSON error response:

{
  "error": {
    "code": "invalid_request",
    "message": "Invalid repository URL",
    "status": 400
  }
}
Note: For efficiency, analysis results are cached for 24 hours. If you've made changes to your repository and want to update the data, you can run a new analysis after the cache period expires.