Использование прокси для работы с API
Олександр Л.
11 June 2025
1036
1036
11 June 2025
Application Programming Interface (also widely known as API) is a common practice today for communicating with websites and applications. When using an API, it is easy and structured to exchange functions and data, seamlessly integrating workflows and data streams between different sites, applications, and systems. For example, you can exchange information such as exchange rates, weather forecasts, product price updates, and much more between your website and a data server. API proxy server is needed for working with APIs for several reasons:
- An API may be limited to a maximum number of requests from a single IP per unit of time, and exceeding this limit may result in your IP being blocked;
- Access to the API may be denied due to your geographic region;
- The number of server requests may be heavily restricted. You need a proxy API to ensure that data exchange is not interrupted. In practice, automation of workflow is achieved by inserting code containing the necessary data. The code can be written in various programming languages: Python, Go, Node.js, JavaScript, PHP, and others. We will take Go as an example, which is well suited for web programming. Keep in mind that creating code requires some programming background (knowledge of what code is and how to specify variables within it). Alternatively, hire a specialist to perform specific tasks.
Automating data exchange between API and proxy
Suppose you need to automate the proxy-API connection to get tomorrow’s weather forecast from the public domain Weather.com (we cite this just to create a clear example of the code). A step-by-step guide on how to work with a proxy server, using Go, would look like this:
- Obtain the proxy address in IP:port:username:password format. For example, 123.45.67.89:8080:user123:pass123.
- Register on WeatherAPI.com to get your unique API key and the full address from which you'll receive weather data, for example, https://api.weatherapi.com/v1/forecast.json?key=YOUR_API_KEY&q=Kyiv&days=2, where days=2 indicates the date for acquiring weather data (for example, for tomorrow).
- Create the request code, which in our case will look like:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func main()
{
// === Step 1: Configure proxy ===
proxyStr := "http://user123:[email protected]:8080"
proxyURL, err := url.Parse(proxyStr)
if err != nil {
fmt.Println("Error in proxy address:", err)
return
}
// === Step 2: Set up HTTP client with proxy ===
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
client := &http.Client{Transport: transport}
// === Step 3: Create weather API request ===
apiKey := "abc123456789xyz" // ← your API key
city := "Kyiv" // ← your city, in this case Kyiv.
apiURL := fmt.Sprintf("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=2", apiKey, city)
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// === Step 4: Send request through proxy (in JSON format) ===
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
// === Step 5: Read and display the received response ===
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
fmt.Println("API response:")
fmt.Println(string(body))
}
By using the proxy API service, after running this code, you will be able to obtain the required data.
Note that writing and processing code in any programming language requires you to have a suitable development environment. In our example, this involves installing the Go programming language on your OS. For editing and parsing JSON responses, you might use Visual Studio Code, GoLand, or Sublime Text/Notepad++ (with syntax highlighting). Additionally, you will need a configured proxy server. Also, a command line (Windows) or Terminal (MacOS/Linux) is necessary to run the written code.
