Bing News API

Matthew Zoljan 120 Reputation points
2024-12-03T17:02:00.3166667+00:00

I am creating a site that allows users to search for articles and that is working fine, I am getting a lot of results. However, when I use the endpoints for U.S latest news, breaking world wide news, and world wide news, I only get 12 results per endpoint. Is this because I am using the free tier? If so why does it allow me to search for thousands but only allows 12 for the above endpoints?

Bing News Search
Bing News Search
A Bing service that supports searching for news and get comprehensive results.
51 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Matthew Zoljan 120 Reputation points
    2024-12-04T17:36:01.7633333+00:00

    I have used the correct category for my endpoint but it still shows 12 results. I have tried using postman too and same results. I want it to show a list in pagenation.

            public async Task OnGetAsync(int pageNumber = 1)
            {
                PageNumber = pageNumber;
                int pageSize = 10; // Number of articles per page
                int offset = (PageNumber - 1) * pageSize;
    
                try
                {
                    string apiKey = "be1137cec3e34918ba30c5ce7495ea42";
                    string apiUrl = $"https://api.bing.microsoft.com/v7.0/news?category=US&count={pageSize}&offset={offset}&mkt=en-US";
    
                    using (HttpClient client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
                        var response = await client.GetAsync(apiUrl);
    
                        if (response.IsSuccessStatusCode)
                        {
                            var responseData = await response.Content.ReadAsStringAsync();
                            var newsData = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseData);
    
                            if (newsData != null && newsData.ContainsKey("value"))
                            {
                                NewsArticles = JsonConvert.DeserializeObject<List<dynamic>>(newsData["value"].ToString());
                                TotalResults = newsData.ContainsKey("totalEstimatedMatches") ? Convert.ToInt32(newsData["totalEstimatedMatches"]) : 0;
                                TotalPages = (int)Math.Ceiling((double)TotalResults / pageSize);
                            }
                            else
                            {
                                ViewData["Message"] = "No news articles found.";
                            }
                        }
                        else
                        {
                            string errorContent = await response.Content.ReadAsStringAsync();
                            _logger.LogError($"API Request failed. Status code: {response.StatusCode}, Response: {errorContent}");
                            ViewData["Message"] = "An error occurred while fetching news from the Bing News API.";
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "An error occurred while fetching news from Bing News API.");
                    ViewData["Message"] = "An error occurred. Please try again later.";
                }
    
    
    0 comments No comments

  2. Matthew Zoljan 120 Reputation points
    2024-12-04T18:03:37.9466667+00:00

    I worked it out. Thanks.


  3. Matthew Zoljan 120 Reputation points
    2024-12-06T22:01:35.2533333+00:00

    Breaking news

    string apiUrl = $"https://api.bing.microsoft.com/v7.0/news/search?q=breaking+news+OR+urgent+OR+attack+OR+emergency&count={pageSize}&offset={offset}&mkt=en-US&sortBy=Date&freshness=Day";
    

    World wide news

    string apiUrl = $"https://api.bing.microsoft.com/v7.0/news/search?q=breaking+world+news+OR+crisis+OR+disaster+OR+war&count={pageSize}&offset={offset}&mkt=en-US&sortBy=Date&freshness=Day";
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.