# API + Perigee

Grab the code from the blueprint section for API hosting with Perigee

{% content-ref url="/pages/lNGf3TZSO5B97YPpPNWn" %}
[Perigee With .NET Hosting](/blueprints/perigee-with-.net-hosting.md)
{% endcontent-ref %}

## Admin controller example

In this example we create a controller that allows us to turn threads on and off.&#x20;

1. Simply add a new empty "API Controller" to the **`Controllers`** folder.&#x20;
2. Call it <mark style="color:blue;">**`AdminController.cs`**</mark>
3. Paste the code below in to replace the content&#x20;

```csharp
using Microsoft.AspNetCore.Mvc;
using Perigee;
using Perigee.Helpers;

// Visit https://docs.perigee.software to learn more
// Visit https://perigee.software to purchase a license!

namespace PerigeeIntroAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        public ThreadRegistry reg { get; }

        public AdminController(ThreadRegistry tr)
        {
            this.reg = tr;
        }


        [HttpGet]
        public IActionResult Taskcontrol([FromQuery] string task, [FromQuery] bool start)
        {
            //Does this thread exist?
            if (reg.ContainsThreadByName(task))
            {
                if (start)
                {
                    reg.StartIfNotRunning(task); //Start if not started
                    return Ok();
                }
                else
                {
                    reg.QueueStop(task, true); //Stop and do not auto restart
                    return Ok();
                }
            }

            return BadRequest();
        }

        [Route("cron")]
        [HttpGet]
        public IActionResult cron([FromQuery] string name, [FromQuery] string cron, [FromQuery] string result)
        {
            if (reg.ContainsThreadByName(name) || string.IsNullOrEmpty(cron) || string.IsNullOrEmpty(result)) return BadRequest();

            reg.AddManagedThread(new ManagedThread(name, (ct, l) =>
            {
                l.LogInformation("result: {result}", result);
            }, cron, reg.CTS, reg.GetLogger<AdminController>()));

            return new JsonResult(new { Message = $"Will run a CRON thread named {name} {Cron.Parse(cron).ToString()}" });
        }

        [Route("delete")]
        [HttpDelete]
        public IActionResult remove([FromQuery] string name)
        {
            reg.RemoveThreadByName(name);
            return Ok();
        }

        [Route("list")]
        [HttpGet]
        public IActionResult list()
        {
            return new JsonResult(new { Threads = reg.GetThreads().Select(f => new { Name = f.Name, Runtime = f.RunTime }).ToArray() });
        }

        [Route("reboot")]
        [HttpGet]
        public IActionResult reboot()
        {
            reg.RestartAllThreads();
            return Ok();
        }

        [Route("exec")]
        [HttpGet]
        public IActionResult exec([FromQuery] string name)
        {
            reg.ExecuteCRONNow(name);
            return Ok();
        }


    }
}

```

In this example we've created a simple endpoint we can call to turn anything on or off by simply checking the thread registry for the requested thread, then turning it on or off.&#x20;

The port may be different - but you can see how easy it would be to turn something on or off!

`[GET] https://localhost:5001/api/admin?task=ProcessAgent&start=true`

{% hint style="warning" %}
Typically speaking this would be secured or unavailable outside the firewall. Don't forget to secure your endpoints!
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.perigee.software/examples-and-demos/api-+-perigee.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
