# API + Perigee

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

{% content-ref url="../blueprints/perigee-with-.net-hosting" %}
[perigee-with-.net-hosting](https://docs.perigee.software/blueprints/perigee-with-.net-hosting)
{% 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 %}
