Skip to content

jellydator/newsapi-go

Repository files navigation

newsapi

GoDoc Coverage Status Go Report Card

Go client implementation for the NewsAPI.

Installation

go get github.com/jellydator/newsapi-go

Usage

Simply, we create a client using NewClient function, by default only an API key is required, however some other parameters can be set using variadic option functions.

client := newsapi.NewClient("apiKey", newsapi.WithHTTPClient(&http.Client{
	Timeout: 5 * time.Second,
}))

Endpoints

Everything

Everything retrieves all articles based on provided parameters. Full endpoint documentation can be viewed here.

articles, pageCount, err := client.Everything(context.Background(), newsapi.EverythingParams{
	Query: "cryptocurrency",
})
if err != nil {
	// handle error
}
// success

Top Headlines

TopHeadlines retrieves top headlines articles based on provided parameters. Full endpoint documentation can be viewed here.

articles, pageCount, err := client.TopHeadlines(context.Background(), newsapi.TopHeadlinesParams{
	Query: "cryptocurrency",
})
if err != nil {
	// handle error
}
// success

Sources

Sources retrieves available sources based on provided parameters. Full endpoint documentation can be viewed here.

sources, err := client.Sources(context.Background(), newsapi.SourceParams{
	Categories: []newsapi.Category{
		newsapi.CategoryBusiness,
		newsapi.CategoryScience,
	},
})
if err != nil {
	// handle error
}
// success