Backend Development 6 min read

Building a City Weather Forecast Service with NestJS, Axios, and QWeather API

This tutorial demonstrates how to create a NestJS backend that converts Chinese city names to pinyin, retrieves the corresponding city ID from the QWeather API, and returns a 7‑day weather forecast using a unified Axios HTTP client configuration.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Building a City Weather Forecast Service with NestJS, Axios, and QWeather API

We implemented a city weather forecast service using the free QWeather API, which allows up to 1,000 requests per day—sufficient for most projects.

First, register on the QWeather platform, bind an email and phone number, and create a project to obtain an API key.

To query a city's 7‑day forecast, you need the city's numeric ID. The ID can be retrieved by converting the Chinese city name to pinyin (using the pinyin package) and calling the QWeather city lookup endpoint.

Example request for a city ID: https://devapi.qweather.com/v7/weather/7d?location=101120606&key=YOUR_API_KEY

After obtaining the city ID, request the weather data: https://api.qweather.com/v7/weather/7d?location=101120201&key=YOUR_API_KEY

Next, set up the NestJS project:

npm install -g @nestjs/cli
nest new city-weather

Install the pinyin conversion library and its TypeScript definitions:

npm install --save pinyin@alpha
npm install --save-dev @types/pinyin

Create a simple endpoint to test the pinyin conversion:

@Get('pinyin')
pinyin(@Query('text') text: string) {
return pinyin(text).join('');
}

Run the service with:

npm run start:dev

To make HTTP calls to third‑party APIs, install the NestJS Axios wrapper:

npm install --save @nestjs/axios axios

Import HttpModule in AppModule and configure a shared Axios instance (baseURL, timeout, etc.). Then inject HttpService where needed.

Weather endpoint implementation (using RxJS firstValueFrom to convert the Observable returned by HttpService into a Promise):

@Inject(HttpService)
private httpService: HttpService;
@Get('weather/:city')
async weather(@Param('city') city: string) {
const cityPinyin = pinyin(city, { style: 'normal' }).join('');
const { data } = await firstValueFrom(this.httpService.get(`https://geoapi.qweather.com/v2/city/lookup?location=${cityPinyin}&key=YOUR_API_KEY`));
const location = data?.['location']?.[0];
if (!location) {
throw new BadRequestException('No matching city information');
}
const { data: weatherData } = await firstValueFrom(this.httpService.get(`https://api.qweather.com/v7/weather/7d?location=${location.id}&key=YOUR_API_KEY`));
return weatherData;
}

This endpoint first converts the input city name to pinyin, looks up the city ID via QWeather, and then fetches the 7‑day forecast for that ID. Errors are returned as HTTP 400 when the city cannot be found.

Overall, the tutorial shows how to combine NestJS, Axios, RxJS, and a pinyin conversion library to build a functional backend weather service.

backendAxiosAPI IntegrationNestJSpinyinWeather API
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.