Announcing Appwrite’s New .NET SDK
Tags: appwrite, dotnet, csharp, opensource
Published: 2023-09-01 18:14:12

With the launch of Appwrite 1.4, we're excited to share that we now have a **.NET SDK** available to build with Appwrite. This is a server-side SDK that targets .NET Standard 2.0, supporting a wide range of SDK versions across .NET, .NET Core, and .NET Framework.

## Steps to Install

Currently, the SDK is distributed through the [NuGet Package Manager](https://www.nuget.org/packages/Appwrite).

You can use the Command Line to install the package in your project using the following commands:

```bash
# Package Manager

Install-Package Appwrite -Version 0.5.0

# or .NET CLI

dotnet add package Appwrite --version 0.5.0
```

Please make sure to add a reference to the package in your project’s `.csproj` file, if it isn’t present already.

```xml
<PackageReference Include="Appwrite" Version="0.5.0" />
```

## Getting Started with the SDK

### Initializing the SDK

Once you have installed the package, you can import the package in your code and set your Appwrite credentials to start making API calls.

```csharp
using Appwrite;

var client = new Client()
	.SetEndpoint("http://cloud.appwrite.io/v1")
	.SetProject("5ff3379a01d25")
	.SetKey("cd868db89");
```

### Consuming Appwrite Services

Once your SDK is initialized, you can start consuming the various APIs offered by Appwrite. Here are some examples:

#### Create a User

```csharp
using Appwrite;
using Appwrite.Services;
using Appwrite.Models;

var users = new Users(client);
var user = await users.Create(
	userId: ID.Unique(),
	email: "email@example.com",
	password: "password",
	name: "name");
```

#### Add a Document To Your Database Collection

```csharp
using Appwrite;
using Appwrite.Services;
using Appwrite.Models;

var databases = new Databases(client);
var document = await databases.CreateDocument(
	databaseId: "[DATABASE_ID]",
	collectionId: "[COLLECTION_ID]",
	documentId: "[DOCUMENT_ID]",
	data: [object]);
```

#### Upload a File

```csharp
using Appwrite;
using Appwrite.Services;
using Appwrite.Models;

var storage = new Storage(client);
var file = await storage.CreateFile(
	bucketId: "[BUCKET_ID]",
	fileId: "[FILE_ID]",
	file: new File("./path-to-files/image.jpg"));
```

#### Trigger a Function Execution

```csharp
using Appwrite;
using Appwrite.Services;
using Appwrite.Models;

var functions = new Functions(client);
var execution = await functions.CreateExecution(
      functionId: "[FUNCTION_ID]",
      body: "[REQUEST_BODY]",
      method: "POST",
      path: "/");
```

### Error Handling

The Appwrite .NET SDK throws an `AppwriteException` object, which includes `Message`, `Code`, and `Response` properties. You can handle any errors by catching `AppwriteException` and presenting the `Message` to the user or using the [error types](https://appwrite.io/docs/response-codes#errorTypes) provided by Appwrite to decide the right type of error and display custom error messages. Below is an example:

```csharp
using Appwrite;
using Appwrite.Services;
using Appwrite.Models;

var users = new Users(client);
try
{
	var user = await users.Create(
		userId: ID.Unique(),
		email: "email@example.com",
		password: "password",
		name: "name");
}
catch (AppwriteException e)
{
	Console.WriteLine(e.Message);
}
```

## How to Contribute and Share Feedback

In case you discover any bugs/issues, want to share suggestions, or make contributions to the .NET SDK, please visit Appwrite’s [SDK Generator](https://github.com/appwrite/sdk-generator) repository on GitHub.

All contributions, including those by people with commit access, must go through a pull request and be approved by an Appwrite core team member before merging. This is to ensure a proper review of all the contributions. You can learn more by visiting the repo’s [Contribution Guide](https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md).

## Next Steps

We hope you will try the .NET SDK in your upcoming projects and share any thoughts and concerns you have with us. Here are some resources you can use to learn more about Appwrite and start building projects:

- 📜 [Appwrite Docs](https://appwrite.io/docs)
- 💽 [Getting Started for Server SDKs Guide](https://appwrite.io/docs/getting-started-for-server)
- 💬 [Discord Community](https://appwrite.io/discord)
- 📺 [Appwrite YouTube Channel](https://www.youtube.com/@Appwrite)