Japan Law API v2 Go Client Library
Go client library for the Japan Law API v2 (法令API v2), automatically generated from the OpenAPI specification.
Features
- Type-safe Go client for all Japan Law API v2 endpoints
- Automatic handling of path parameters and query parameters
- Custom date/time types for proper date format handling (YYYY-MM-DD)
- Support for both JSON and raw content (XML/HTML) responses
- Helper functions for creating pointer values
- Comprehensive type definitions for all API responses
- Full English documentation and comments
Installation
go get go.ngs.io/jplaw-api-v2
Usage
Basic Example
package main
import (
"fmt"
"log"
lawapi "go.ngs.io/jplaw-api-v2"
)
func main() {
// Create API client
client := lawapi.NewClient()
// Search for laws by title
params := &lawapi.GetLawsParams{
LawTitle: lawapi.StringPtr("電波法"),
Limit: lawapi.Int32Ptr(10),
}
result, err := client.GetLaws(params)
if err != nil {
log.Fatal(err)
}
// Process results
for _, law := range result.Laws {
if law.LawInfo != nil {
fmt.Printf("Law ID: %s\n", law.LawInfo.LawId)
}
if law.RevisionInfo != nil {
fmt.Printf("Law Title: %s\n", law.RevisionInfo.LawTitle)
}
}
}
API Methods
GetLaws
Search and retrieve laws based on various criteria.
params := &lawapi.GetLawsParams{
LawTitle: lawapi.StringPtr("電波法"),
LawType: &[]lawapi.LawType{lawapi.LawTypeAct},
Limit: lawapi.Int32Ptr(100),
}
result, err := client.GetLaws(params)
GetLawData
Retrieve full law data including the law text.
lawID := "325AC0000000131"
params := &lawapi.GetLawDataParams{
ResponseFormat: (*lawapi.ResponseFormat)(lawapi.StringPtr("json")),
}
lawData, err := client.GetLawData(lawID, params)
GetLawFile
Retrieve law file in various formats (XML, JSON, HTML, RTF, DOCX).
lawID := "325AC0000000131"
fileType := "xml" // or "json", "html", "rtf", "docx"
params := &lawapi.GetLawFileParams{}
content, err := client.GetLawFile(lawID, fileType, params)
GetRevisions
Get revision history for a specific law.
lawID := "325AC0000000131"
params := &lawapi.GetRevisionsParams{}
revisions, err := client.GetRevisions(lawID, params)
GetKeyword
Search laws by keyword.
params := &lawapi.GetKeywordParams{
Keyword: "個人情報",
Limit: lawapi.Int32Ptr(50),
}
result, err := client.GetKeyword(params)
GetAttachment
Retrieve attachments from law documents.
revisionID := "325AC0000000131_20260527"
params := &lawapi.GetAttachmentParams{
Src: lawapi.StringPtr("./pict/example.jpg"),
}
attachment, err := client.GetAttachment(revisionID, params)
Code Generation
This library is automatically generated from the OpenAPI specification. To regenerate the client:
Prerequisites
go get gopkg.in/yaml.v3
Download OpenAPI Specification
wget https://laws.e-gov.go.jp/api/2/swagger-ui/lawapi-v2.yaml
Build the Generator
cd cmd/clientgen
go build -o ../../clientgen
Generate the Client
./clientgen -input lawapi-v2.yaml -output . -package lawapi
Generator Options
-input: Path to the OpenAPI specification file (default: “lawapi-v2.yaml”)-output: Output directory for generated files (default: “.”)-package: Package name for generated code (default: “lawapi”)
Project Structure
cmd/clientgen/- Code generation toolmain.go- Entry point for the generatoropenapi.go- OpenAPI specification structuresgenerator.go- Code generation logic
types.go- Generated type definitionsclient.go- Generated HTTP client and API methodsexample/- Usage examplesmain.go- Basic usage exampletest_path_params.go- Path parameters example
Date Handling
The library includes custom Date and DateTime types to handle the API’s date formats:
Date: Handles dates in “YYYY-MM-DD” formatDateTime: Handles both RFC3339 and “YYYY-MM-DD” formats
Helper Functions
The library provides helper functions for creating pointer values:
lawapi.StringPtr("value") // *string
lawapi.IntPtr(42) // *int
lawapi.Int32Ptr(100) // *int32
lawapi.Int64Ptr(1000) // *int64
lawapi.BoolPtr(true) // *bool
lawapi.Float32Ptr(3.14) // *float32
lawapi.Float64Ptr(2.718) // *float64
Type Definitions
The library includes comprehensive type definitions for all API responses:
LawsResponse- Response for law searchLawDataResponse- Response for law data retrievalKeywordResponse- Response for keyword searchLawRevisionsResponse- Response for revision historyLawInfo- Basic law informationRevisionInfo- Law revision informationLawItem- Individual law entryKeywordItem- Keyword search result item- And many more…
Enumerations
Type-safe enumerations for various API parameters:
LawType- Constitution, Act, CabinetOrder, etc.LawNumEra- Meiji, Taisho, Showa, Heisei, ReiwaResponseFormat- json, xmlFileType- xml, json, html, rtf, docxRepealStatus- None, Repeal, Expire, Suspend, LossOfEffectiveness- And more…
Error Handling
All API methods return an error as the second return value:
result, err := client.GetLaws(params)
if err != nil {
log.Printf("API error: %v", err)
return
}
Custom HTTP Client
You can provide a custom HTTP client for advanced configurations:
client := lawapi.NewClient()
customHTTPClient := &http.Client{
Timeout: 60 * time.Second,
// Add custom transport, etc.
}
client.SetHTTPClient(customHTTPClient)
License
This client library is generated from the public Japan Law API v2 specification. Please refer to the official API terms of use for usage guidelines.
Contributing
Issues and pull requests are welcome. To regenerate the client after updating the generator:
- Make changes to the generator in
cmd/clientgen/ - Rebuild the generator:
go build -o clientgen cmd/clientgen/*.go - Regenerate the client:
./clientgen -input lawapi-v2.yaml -output . -package lawapi - Test the changes with examples in
example/