Skip to content

Creating APIs with CoPilot in Visual Studio

This is inspired vy a YouTube video by Patrick God, which I have extended a little.

Overview

CoPilot can be used to do a lot of the heavy lifting when creating an API. The following explains the steps to take to achieve this, but as always check the generated code and make sure you understand what is happening.

1. Create the API project in Visual Studio

The more information and fundamentals you can provide to CoPilot, the more accurate the generated results will be. With that in mind it is probably a good idea to seed the process by creating an *ASP.NET Core Web API project manually.

Configure Web API project

Additional Project Information

With the basics in place we can jump into the CoPilot Window to continue.

2. Ensure CoPilot is Configured Correctly

Set CoPilot Chat mode to the Agent Mode:

Set Agent Mode

And the best model (at the time of writing) is probably Claude Sonnet 4.5 (if available - this is currently paid for) as this provides more feedback than other models, so explains what it is going to do better.

Set CoPilot Model


NOTE: Sometimes you may need to switch back to GPT is you get an issue, or fix manually.


3. Start Prompting

Now we can start prompting the AI in plain English, giving as much detail and context as possible, for example:

I would like to build a video game library API, please create a suitable model for that.

This vague prompt results in something like the following:

Initial Prompt Part 1 Initial Prompt Part 2

You can click on the individual files in the output to check over hat was created, and if you are happy you should accept (keep) the generated code before continuing with additional prompts.

Now we can flesh it out a bit with an additional prompt:

Please create all CRUD operations for the Game entity, and use a mock video game list probably a static list to be able to test this.

This generates a bunch of files including the controller and a mock service implementation. These files should be manually reviewed before choosing to keep them (or not)

The solution can then be tested via the .http file or a tool like Postman or Insomnia.

To add Swagger or/and Scalar you simply issue another prompt, something along the lines of:

Please add Swagger documentation to the solution.


NOTE: I actually tried running before doing this, so couldn't get to the Swagger endpoint. Where I misread the CoPilot output chat window, I incorrectly assumed it had already added Swagger, a good example of the problem with not reading and taking the time to understand the generated code - my bad. However, I did take the opportunity to use CoPilot to fix the issue by running the following prompt:

I'm trying to debug this project, but when I try to navigate to the swagger endpoint I see the following error. How can I fix this?

This localhost page can’t be found No webpage was found for the web address: https://localhost:7073/ HTTP ERROR 404

This did the analysis, found the issue and added Swagger with appropriate configuration.


Add Entity Framework

We can now add Entity framework via some prompting:

Please add Entity Framework and use a local SQL Server instance. The connection string would start with "Server=localhost,1433" and I would like the database name to be CoPilotApiTest. Keep the controller thin by moving all the business logic by taking a repository style approach, using dependency injection with best practices and a primary constructor so that the controller has IGameService injected, and this service injects the DbContext from Entity Framework.

This is a good example of just how descriptive you can (and should) be when prompting in this way.

To be fair we should have probably split this into two prompts, the first to convert this to a repository, the second to add Entity Framework. This would give us an easier route to verify the code generated without getting overloaded with changes.

The result of running the above is CoPilot will make the necessary changes to the solution files, run multiple builds to test, and create some documentation in markdown files.

Adding tests to the .http file

I then added some tests to the .http file by issuing the following command in the editor window itself via [Alt] + '/' (I could have done this via the chat window), which added the following (and a whole lot more, this is just a snippet):

# Get all games
GET {{CoPilotGeneratedApiTest_HostAddress}}/api/games
Accept: application/json

###

# Get game by ID
GET {{CoPilotGeneratedApiTest_HostAddress}}/api/games/1
Accept: application/json

###

# Get game by ID - Not Found
GET {{CoPilotGeneratedApiTest_HostAddress}}/api/games/999
Accept: application/json

###

# Create new game
POST {{CoPilotGeneratedApiTest_HostAddress}}/api/games
Content-Type: application/json

{
  "title": "The Legend of Zelda: Breath of the Wild",
  "genre": "Action-Adventure",
  "platform": "Nintendo Switch",
  "releaseDate": "2017-03-03",
  "price": 59.99
}

###

This was all great, however when I tried sending the requests I got the following output:

.http requests failing

So I issued the following prompt to CoPilot:

The requests are failinbg in my .http file with the following error, please fix this:

Awaiting response ...

❌Errors:

The SSL connection could not be established, see inner exception.

CoPilot instructed me that I was using:

@CoPilotGeneratedApiTest_HostAddress = http://localhost:5271

instead of:

@CoPilotGeneratedApiTest_HostAddress = https://localhost:7073

While running in https mode, so suggested the fix. This still didn't work, I told it so, and it suggested using the original configuration line but running in http mode instead of https, and reoving the line to redirect all http requests to https whilst in Development mode. This finally worked...

...however on running one of the tests it provided a literal string value in thew JSON message body, rather than the Id of that value as was required. This one I fixed manually, along with checking the Id in the URL matched the Id in the body - another example of the need to review and test the generated code.

Adding a test project

So not let's get together somy testing. I issues the following prompt:

Now please add a test project which uses xUnit to teast all the crud methods of the GamesController and makes use of an in memory database rather than relying on a real SQL Server instance. Please follow best practices.

And CoPilot went away and did its magic.

See Also