home / skills / windmill-labs / windmill / write-script-csharp

This skill helps you craft C# scripts with a public static Main method, return types, and NuGet integration for quick automation.

npx playbooks add skill windmill-labs/windmill --skill write-script-csharp

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.1 KB
---
name: write-script-csharp
description: MUST use when writing C# scripts.
---

## CLI Commands

Place scripts in a folder. After writing, run:
- `wmill script generate-metadata` - Generate .script.yaml and .lock files
- `wmill sync push` - Deploy to Windmill

Use `wmill resource-type list --schema` to discover available resource types.

# C#

The script must contain a public static `Main` method inside a class:

```csharp
public class Script
{
    public static object Main(string name, int count)
    {
        return new { Name = name, Count = count };
    }
}
```

**Important:**
- Class name is irrelevant
- Method must be `public static`
- Return type can be `object` or specific type

## NuGet Packages

Add packages using the `#r` directive at the top:

```csharp
#r "nuget: Newtonsoft.Json, 13.0.3"
#r "nuget: RestSharp, 110.2.0"

using Newtonsoft.Json;
using RestSharp;

public class Script
{
    public static object Main(string url)
    {
        var client = new RestClient(url);
        var request = new RestRequest();
        var response = client.Get(request);
        return JsonConvert.DeserializeObject(response.Content);
    }
}
```

Overview

This skill provides clear conventions and examples for writing C# scripts that run in the platform. It explains the required entry point, how to return values, and how to reference NuGet packages. Use this guidance to create scripts that can be packaged, metadata-generated, and deployed via the CLI.

How this skill works

A valid script must define a public static Main method inside any class; the method accepts typed parameters and returns either object or a concrete type. NuGet libraries are added with #r "nuget: ..." directives at the top of the file and standard using statements. Once scripts are written, you generate metadata and push deployments with the CLI so the platform can run them as webhooks, workflows, or UI actions.

When to use it

  • Building server-side logic in C# to run as webhooks or workflow tasks.
  • Calling external APIs or processing data with NuGet libraries.
  • Creating typed entry points that accept parameters from the platform.
  • Packaging scripts for automated deployment via the CLI.
  • Prototyping integrations that need .NET ecosystem libraries.

Best practices

  • Always declare a public static Main method; the class name does not matter.
  • Return object for flexible payloads or a specific type for strict contracts.
  • Place #r "nuget: Package, Version" directives at the top, followed by using statements.
  • Keep parameter types explicit to make metadata generation accurate.
  • Test locally, then run `wmill script generate-metadata` and `wmill sync push` to deploy.

Example use cases

  • Transform incoming JSON into typed objects and return structured responses.
  • Call a REST API using RestSharp and deserialize results with Newtonsoft.Json.
  • Fetch or update records in a database via a NuGet client library.
  • Create a small computation or aggregation endpoint exposed as a webhook.
  • Wrap existing .NET logic as a script to reuse across workflows and UIs.

FAQ

Does the class name matter?

No. Only the public static Main method signature matters; the class name can be anything.

How do I add external NuGet packages?

Add them with #r "nuget: PackageName, Version" at the top of the file, then include using statements as usual.

What CLI steps are required to deploy a script?

After writing the script, run `wmill script generate-metadata` to produce .script.yaml and lock files, then `wmill sync push` to deploy.