If you’re specifically interested in ZIP codes in the United States, as opposed to postal codes elsewhere, that would be somewhat difficult to do with OSM data alone. We do map boundaries of cities, villages, etc., but ZIP codes are delivery routes, not boundaries, which means we can’t really map them as such. We do collect individual addresses, including their postal codes, so this query returns the municipal boundary that contains all the addresses of ZIP code 02134.
That query returns Boston, which contains the entire ZIP code. However, many ZIP codes are associated with a neighboring city where the main post office is located. In many cases, you can also query for U.S. Post Office branches by the ZIP codes they serve, for example, post offices serving ZIP code 47579. The returned feature has tags indicating a postal city of Santa Claus, Indiana. With either of these queries, you can either run the query in your browser at these links or click the Export button to get a link to an API endpoint.
If your goal is to come up with the best postal city name associated with a given ZIP code and accuracy is important, these queries may not give you the best results. For example, the USPS will accept Boston for ZIP code 02134, but they prefer to call it Allston. In some cases, the city that physically includes the address or the post office is not an acceptable postal city name. You might be able to determine this by getting all the addresses matching a given ZIP code in addr:postcode=*
and figuring out the most common value of addr:city=*
on these features.
Other addressing-specific datasets, such as OpenAddresses, may be better-suited for this kind of use case. The USPS also has its own API, if you don’t mind paying for it.