Using Public API Docs in JavaScript with https://business.usa.gov/developer

The https://business.usa.gov/developer API provides a range of business-related information to developers including industry data, government business opportunities, and finance information. In this blog, we will explore how to use this public API in JavaScript with examples.

Getting Started

To get started, you will need to request an API key. This key will allow you to access and use all of the APIs provided by https://business.usa.gov/developer. You can request a key by going to the API key request page and filling out the form. Once you have an API key, you can start making calls to the API.

Example API Calls

Getting Industry Data

Retrieving data from the Industry API endpoint is very straightforward. Here's how you can retrieve a list of industries:

const api_key = 'YOUR_API_KEY';
const api_url = 'https://api.data.gov/ed/industries/v1/industries/';
const api_params = { api_key: api_key };
const api_request_url = new URL(api_url);
api_request_url.search = new URLSearchParams(api_params);

fetch(api_request_url)
  .then(response => response.json())
  .then(data => console.log(data));

Getting Government Business Opportunities

Retrieving opportunities from the Government Business Opportunities API endpoint is also very straightforward. Here's how you can retrieve a list of opportunities:

const api_key = 'YOUR_API_KEY';
const api_url = 'https://api.sam.gov/prod/opportunities/v1/search/';
const api_params = { api_key: api_key };
const api_request_url = new URL(api_url);
api_request_url.search = new URLSearchParams(api_params);

fetch(api_request_url)
  .then(response => response.json())
  .then(data => console.log(data));

Getting Finance Information

Retrieving finance information from the Finance API endpoint is a bit more complicated. You will need to send a POST request to the API, along with a JSON payload containing your search criteria. Here's how you can retrieve a list of finance-related information:

const api_key = 'YOUR_API_KEY';
const api_url = 'https://api.usa.gov/business-finance/v2/finance/';
const api_params = { api_key: api_key };

const api_request = {
  api_endpoint: 'get_financial_statement_details',
  sic_seed: '5812',
  assets_range: {
    gt: 100000,
    lt: 500000
  },
  zip_seed: '94115'
};

const api_options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(api_request)
};

fetch(api_url, api_options)
  .then(response => response.json())
  .then(data => console.log(data));

Conclusion

Using public API docs in JavaScript can be a lot of fun and it provides values to developers. In this blog, we've explored how you can use the https://business.usa.gov/developer API. With these examples, you can start building amazing applications that help businesses.

Related APIs in Government