---
title: "Parse an international address with a country hint in C#"
url: https://recursiveai.org/onecontact/cookbook/parse-tier2-address-with-country-hint/
description: "Parse a messy international postal address in C# with OneContact. Pass a country hint and use a local model to split the street, town, and postcode."
version: "1.1.0"
---

# Parse an international address with a country hint in C#

**OneContact** is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneContact.Models) · [GitHub](https://github.com/recursive-ai-oss/onecontact-csharp) · [Docs](https://recursiveai.org/onecontact/docs/) · [Download example (.zip)](https://recursiveai.org/onecontact/cookbook/parse-tier2-address-with-country-hint/parse-tier2-address-with-country-hint.zip)

The `RecursiveAI.OneContact.Models` package ships an offline model that reads addresses the rules can’t. Installing it turns on the fallback automatically; pass the destination country, such as `GB`, because the model doesn’t determine it.

```shell
dotnet add package RecursiveAI.OneContact.Models
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneContact;

var address = PostalAddress.Parse(
    "58 Example Avenue Shrewsbury\nSY4 1AA",
    Region.Parse("GB"));
Console.WriteLine(address.Canonical);
```

[The example](https://recursiveai.org/onecontact/cookbook/parse-tier2-address-with-country-hint/parse-tier2-address-with-country-hint.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

```text
58 EXAMPLE AVENUE, SHREWSBURY, SY4 1AA
```

## Full program

The download uses the invented UK address above, where the town is on the street line and the postcode is alone on the last line. It prints the country from the hint and the parsed delivery fields.

```csharp
using System.Buffers;
using RecursiveAI.OneContact;

const string text = "58 Example Avenue Shrewsbury\nSY4 1AA";
var status = PostalAddress.TryParse(text, Region.Parse("GB"),
    out var address, out var detail);
if (status != OperationStatus.Done)
{
    Console.WriteLine(detail.PrimaryReason.Id);
    return;
}

Console.WriteLine($"Country: {address.Country}");
foreach (var line in address.StreetLines)
    Console.WriteLine($"Street: {line}");
Console.WriteLine($"Town: {address.Locality}");
Console.WriteLine($"Postcode: {address.PostalCode}");
```

`dotnet run` prints:

```text
Country: GB
Street: 58 EXAMPLE AVENUE
Town: Shrewsbury
Postcode: SY4 1AA
```

If the address can’t be parsed, `Parse` throws `ContactParseException`; the full program prints `detail.PrimaryReason.Id` instead.

## Variations

To check that the fallback actually read this input, print each field’s `Source` with the same country hint:

```csharp
using System.Buffers;
using RecursiveAI.OneContact;

var status = PostalAddress.TryParse(
    "58 Example Avenue Shrewsbury\nSY4 1AA",
    Region.Parse("GB"), out _, out var detail);
if (status != OperationStatus.Done)
    throw new FormatException(detail.PrimaryReason.Id);

foreach (var field in detail.Fields)
    Console.WriteLine($"{field.Name}: {field.Source}");
```

```text
street.house-number: Model
street.name: Model
street.type: Model
locality: Model
postal-code: Model
```

## Related recipes

- [Parse messy US and Canadian addresses in C#](https://recursiveai.org/onecontact/cookbook/enable-us-canada-address-model-fallback/)
- [See which address fields came from a model in C#](https://recursiveai.org/onecontact/cookbook/inspect-address-rule-vs-model-fields/)
- [Parse a postal address in C#](https://recursiveai.org/onecontact/cookbook/parse-postal-address/)

## The short answer

Install `RecursiveAI.OneContact.Models` and call `PostalAddress.Parse(text, country)` with the destination country you already know. Use `Region.Parse("GB")` for the United Kingdom, not `UK`. The package covers Tier 1 (the US and Canada) and Tier 2 (the other countries with free-text support, including the UK, Mexico, and Australia). The smaller `RecursiveAI.OneContact.Models.Tier1` package covers only the US and Canada. Check the [free-text coverage list](https://recursiveai.org/onecontact/docs/address-structural/#coverage) before enabling this path for a country.

## What does the country hint mean when parsing an international address?

The hint is the country the address belongs to, not the sender’s country or the language of the address. If your checkout already asks the customer to select a destination country, use that selection. It can stay separate from the free-text address: the example doesn’t append `United Kingdom` to the string just to make the parser recognize it.

The rules try to read the address for that country first. The model can label the words the rules couldn’t read, but it doesn’t classify the country. A Tier 2 model read gets its country from the hint or from a country already decided by the rules. If your input has no trustworthy country value, don’t substitute the country of your own business or the last successful row. Ask for the destination country before relying on the result.

## Is the postal code enough to tell which country an address is in?

No. The [model documentation](https://recursiveai.org/onecontact/docs/models-superset/) gives Germany, Spain, and Czechia as examples of countries whose postcodes can look like a US ZIP code. A code can help read an address once the country is known, but a shared pattern can’t establish which country the address belongs to. Keep the hint as a separate field when you import records so you can rerun a parse with the same context later.

The example shows the code `SY4 1AA` in `PostalCode`, separate from `Locality` (`Shrewsbury`). That split is not a validation against a delivery database. If your input already has separate address fields, use [country-specific formatting](https://recursiveai.org/onecontact/cookbook/format-postal-address/) rather than joining those fields and asking a model to split them again.
