All tutorials

.NET

intermediate

The .NET API wrapper is useful for .NET developers who wish to seamlessly integrate the what3words API into their applications, without the hassle of having to manage the low level API calls themselves.

A full example of a .NET application is available in our GitHub repository.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///index.home.raft to latitude and longitude coordinates such as (-0.203586, 51.521251) and vice versa. It features a powerful AutoSuggest function, which can validate and correct user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, AutoSuggest can be used to post-process voice output.

All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

1
2

Installation

The artifact is available through NuGet Package what3words.dotnet.wrapper

3

Setup

Instantiate an instance of What3WordsV3, from which all API requests can be made

var wrapper = new What3WordsV3("YOUR_API_KEY_HERE");
Copied

If you run our Enterprise Suite API Server yourself, you may specify the URL to your own server like so:

var wrapper = new What3Words("YOUR_API_KEY_HERE", "https://api.yourserver.com")  
Copied
4

Usage

Convert to 3 word address

This function converts coordinates (expressed as latitude and longitude) to a 3 word address.

More information about ConvertTo3wa, including returned results, is available in the what3words REST API documentation.

Find the words for (51.222011, 0.152311):

var result = await wrapper.ConvertTo3WA(new Coordinates(51.222011, 0.152311)).RequestAsync();
Copied

Convert to coordinates

This function converts a 3 word address to a position, expressed as coordinates of latitude and longitude.

This function takes the words parameter as a string of 3 words 'filled.count.soap'

More information about ConvertToCoordinates, including returned results is available in the what3words REST API documentation.

Find the words for ///filled.count.soap:

var result = await wrapper.ConvertToCoordinates("filled.count.soap").RequestAsync();
Copied

AutoSuggest

When presented with a 3 word address that may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural vs singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • One or more countries
  • A geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More information about autosuggest, including returned results is available in the what3words REST API documentation.

Example: Basic AutoSuggest call

var result = await wrapper.Autosuggest("index.home.r").RequestAsync();
Copied

Example: AutoSuggest, Focus on (51.4243877,-0.34745)

var result = await wrapper.Autosuggest("index.home.r", new AutosuggestOptions().SetFocus(51.4243877,-0.34745)).RequestAsync();
Copied

Grid section

Grid Section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More information about GridSection, including returned results is available in the what3words REST API documentation.

Get a grid for (51.222609, 0.152898) in the south-west, and (51.222011, 0.152311 in the north-east:

var result = await wrapper.GridSection(new Coordinates(51.222011, 0.152311), new Coordinates(51.222609, 0.152898)).RequestAsync();
Copied

Available languages

This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.

More information about AvailableLanguages, including returned results is available in the what3words REST API documentation.

var result = await wrapper.AvailableLanguages().RequestAsync();
Copied

Handling errors

Success or failure of an API call can be determined through the use of the IsSuccessful propriety.

If it’s been determined that an API call was not successful, Error type and Message values which represent the error, are accessible from through the Error propriety.

var autosuggestResult = await api.Autosuggest(
  "freshen.overlook.clo",
  new AutosuggestOptions().SetClipToCountry(new List<string> { "fr", "de" })
).RequestAsync();

if (autosuggestResult.IsSuccessful) {
  ...
} else {
  if (autosuggestResult.Error.Error == What3WordsError.BadClipToCountry) {
    // Invalid country clip is provided
    Console.WriteLine($"BadClipToCountry: {autosuggestResult.Error.Message}");
  }
}
Copied
5

RegEx functions

This section introduces RegEx functions that can assist with checking and finding possible what3words addresses in strings. The three main functions covered are:

  • isPossible3wa – Match what3words address format;
  • findPossible3wa – Find what3words address in Text;
  • isValid3wa – Verify a what3words address with the API;

isPossible3wa

Our API wrapper RegEx function “isPossible3wa” can be used used to detect if a text string (like “filled.count.soap“) in the format of a what3words address without having to ask the API. This functionality checks if a given string could be a what3words address. It returns true if it could be, otherwise false.

Note: This function checks the text format but not the validity of a what3words address. Use isValid3wa to verify validity.

using System;
using what3words.dotnet.wrapper;

namespace What3WordsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the What3Words API with your API key
            var api = new What3WordsV3("YOUR_API_KEY");

            // Example what3words addresses
            string[] addresses = { "filled.count.soap", "not a 3wa", "not.3wa address" };

            // Check if the addresses are possible what3words addresses
            foreach (var address in addresses)
            {
                bool isPossible = api.IsPossible3wa(address);
                Console.WriteLine($"Is '{address}' a possible what3words address? {isPossible}");
            }
        }
    }
}
Copied

Expected Output

  • isPossible3wa(“filled.count.soap”) returns true
  • isPossible3wa(“not a 3wa”) returns false
  • isPossible3wa(“not.3wa address”)returns false

findPossible3wa

Our API wrapper RegEx function “findPossible3wa” can be used to detect a what3words address within a block of text, useful for finding a what3words address in fields like Delivery Notes. For example, it can locate a what3words address in a note like “Leave at my front door ///filled.count.soap”. The function will match if there is a what3words address within the text. If no possible addresses are found, it returns an empty list.

Note:

  • This function checks the text format but not the validity of a what3words address. Use isValid3wa to verify validity.
  • This function is designed to work across languages but do not work for Vietnamese (VI) due to spaces within words.
using System;
using what3words.dotnet.wrapper;

namespace What3WordsExample
class Program
{
    static void Main(string[] args)
    {
        // Initialize the what3words API with your API key
        var api = new What3WordsV3("YOUR_API_KEY");

        // Example texts
        string[] texts = {
            "Please leave by my porch at filled.count.soap",
            "Please leave by my porch at filled.count.soap or deed.tulip.judge",
            "Please leave by my porch at"
        };

        // Check each text for possible what3words addresses
        foreach (var text in texts)
        {
            List<string> possibleAddresses = api.FindPossible3wa(text);
            Console.WriteLine($"Possible what3words addresses in '{text}': {string.Join(", ", possibleAddresses)}");
        }
    }
}
Copied

Expected Output

  • findPossible3wa(“Please leave by my porch at filled.count.soap”) returns ['filled.count.soap']
  • findPossible3wa(“Please leave by my porch at filled.count.soap or deed.tulip.judge”) returns ['filled.count.soap', 'deed.tulip.judge']
  • findPossible3wa(“Please leave by my porch at”) returns []

isValid3wa

Our API wrapper RegEx function “isValid3wa” can be used to determine if a string is a valid what3words address by checking it against the what3words RegEx filter and verifying it with the what3words API.

using System;
using what3words.dotnet.wrapper;

namespace What3WordsExample

class Program
{
    static void Main(string[] args)
    {
        // Initialize the what3words API with your API key
        var api = new What3WordsV3("YOUR_API_KEY");

        // Example addresses
        string[] addresses = {
            "filled.count.soap",
            "filled.count.",
            "coding.is.cool"
        };

        // Check if the addresses are valid what3words addresses
        foreach (var address in addresses)
        {
            bool isValid = api.IsValid3wa(address);
            Console.WriteLine($"Is '{address}' a valid what3words address? {isValid}");
        }
    }
}
Copied

Expected Outputs

  • isValid3wa(“filled.count.soap”) returns True
  • isValid3wa(“filled.count.”) returns False
  • isValid3wa(“coding.is.cool”) returns False

Also make sure to replace <YOUR_API_KEY> with your actual API key. These functionalities provide different levels of validation for what3words addresses, from simply identifying potential addresses to verifying their existence on Earth.

Server and ScriptingWebsiteAdd a 3 word address input fieldBatch convert 3 word addresses or co-ordinatesDetect if text looks like a 3 word addressDisplay a 3 word addressUse 3 word addresses within an address search.NET

Related tutorials