Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
How do I make API call for city name instead of zip code?

I am utilizing API calls from https://openweathermap.org/current

As of now, I have a program that displays the weather after a zip code is inserted. I want to change the program to display weather after a city name is inserted instead of a zip code.

Here is the code I have written for the zip code input which works:

// takes in the zip from the html form, display in // console. Takes in as string, 
ex. for zip 02139
var zip = String(req.body.zipInput);
console.log(req.body.zipInput);

//build up the URL for the JSON query, API Key is // secret and needs to be obtained 
by signup
const units = "imperial";
const apiKey = "ed83ec6f91552fa762538146eef4e349";
const url = "https://api.openweathermap.org/data/2.5/weather?zip=" + zip + "&units=" 
+ units + "&APPID=" + apiKey;

Here is the code I have written for the city name which does NOT work:

// takes in the city name from the html form, display in // console. Takes in as 
string.
var cityname = String(req.body.citynameInput);
console.log(req.body.citynameInput);

//build up the URL for the JSON query, API Key is // secret and needs to be obtained 
by signup
const units = "imperial";
const apiKey = "ed83ec6f91552fa762538146eef4e349";
const url = "http://api.openweathermap.org/geo/1.0/direct?q={city name}&limit=. 
{limit}&appid={API key}" + cityname + "&units=" + units + "&APPID=" + apiKey;

How would I properly write the code to get weather information after a city name is inserted instead of a zip code? The code that I have written for the city name is not working like the code I have written for the zip code.

Please give the answer in code! Feel free to copy and paste my code and make corrections where needed. Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Everything seems to be in the doc https://openweathermap.org/api/geocoding-api#direct_name just replace the orange parts by actual value either using template literal or concatenation (just as you did for zip).

Here you are missing the city name in your url.

PS: Don't forget that city name seems to contains mutliple value

EDIT: thanks @Bravo for the corrections!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!