---
title: "Extract a social media handle from a URL in C#"
url: https://recursiveai.org/onecontact/cookbook/find-social-profile-handle-in-url/
description: "Extract a social media handle from an X or Instagram profile URL in C#. Use OneContact’s profile flag and parsed path without a network call."
version: "1.0.0"
---

# Extract a social media handle from a URL 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) · [GitHub](https://github.com/recursive-ai-oss/onecontact-csharp) · [Docs](https://recursiveai.org/onecontact/docs/) · [Download example (.zip)](https://recursiveai.org/onecontact/cookbook/find-social-profile-handle-in-url/find-social-profile-handle-in-url.zip)

Read X (Twitter) or Instagram handles without opening the link.

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

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 url = WebAddress.Parse(
    "https://x.com/Example_Name?ref=card");
if (url.Flags.HasFlag(WebFacts.SocialX))
    Console.WriteLine(url.Path.Trim('/'));
```

Unzip [the example](https://recursiveai.org/onecontact/cookbook/find-social-profile-handle-in-url/find-social-profile-handle-in-url.zip), then `dotnet run`.

```text
Example_Name
```

This recipe extracts X (Twitter) and Instagram handles only. OneContact recognizes other profile shapes, such as YouTube channel URLs, but the example doesn’t extract handles from them.

## Full program

```csharp
using RecursiveAI.OneContact;

string[] links =
[
    "https://x.com/Example_Name?ref=card",
    "https://www.instagram.com/Example.Name/",
    "https://x.com/home",
    "https://www.instagram.com/p/example/",
];

foreach (string link in links)
{
    if (!WebAddress.TryParse(link, out var url))
        Console.WriteLine("Invalid URL");
    else if ((url.Flags & (WebFacts.SocialX | WebFacts.SocialInstagram)) != 0)
        Console.WriteLine($"Handle: {url.Path.Trim('/')}");
    else
        Console.WriteLine("No recognized X or Instagram profile");
}
```

`dotnet run` prints:

```text
Handle: Example_Name
Handle: Example.Name
No recognized X or Instagram profile
No recognized X or Instagram profile
```

A URL that won’t parse is reported separately from a valid URL without either profile flag. A flag describes the link’s shape, not whether the account exists.

## Variations

If you only need to tell which profile shape matched, read the flags without extracting the name:

```csharp
using RecursiveAI.OneContact;
var url = WebAddress.Parse(
    "https://instagram.com/Example.Name/");
Console.WriteLine(
    url.Flags.HasFlag(WebFacts.SocialInstagram));
```

```text
True
```

## Related recipes

- [Parse a URL](https://recursiveai.org/onecontact/cookbook/parse-url/)
- [Get the domain from a URL](https://recursiveai.org/onecontact/cookbook/get-domain-from-url/)

## The short answer

To extract a handle from an X or Instagram profile URL in C#, parse it with `WebAddress`, check `WebFacts.SocialX` or `WebFacts.SocialInstagram`, then trim the slashes from `Path`. For those two flags, OneContact found one nonempty handle segment. It supplies the flag and the parsed path, not a separate handle property. The example returns `Example_Name` without looking up the account.

## How do you extract a username from a profile link?

Read the parsed path instead of splitting the original URL on `/`. `Path` is separate from the query string, so `?ref=card` never becomes part of the handle. It also preserves the path’s case. In the second example, the trailing slash comes off with `Trim('/')`, and the period in `Example.Name` stays.

Check the profile flag before trimming. The [profile rules](https://recursiveai.org/onecontact/docs/web/#social-profile-shapes) check the host and the path for HTTP and HTTPS URLs. For X, including `twitter.com` links, the pattern accepts one segment of up to 15 letters, digits or underscores, with exclusions for known site pages such as `home`. Instagram’s single segment can also contain periods, with a maximum of 30 characters. Its `/p/` post links are not profile matches.

## Do all social profile URLs contain a handle?

No. OneContact also recognizes shapes such as YouTube’s `/channel/<id>` and LinkedIn’s `/company/<name>`. The first is a channel ID and the second is a company slug; neither should pass through the single-segment extraction above. If you accept another network, check its own flag and handle only the path forms you want to support. Keep the platform alongside any extracted handle when using it as a lookup field.

The recognition is local. It doesn’t follow redirects, check whether an account exists, or tell you whether someone controls it. Leave the handle blank when the URL doesn’t match a shape your application handles; a nonempty path alone is not a username.
