New API Command

Scaffolds out API files and projects based on a given template file in a json or yaml format.

The Command

craftsman new:api [options] <filepath>

Arguments

ArgumentDescription
filepathThe full filepath for the yaml or json file that describes your web API using a proper Wrapt format.

Options

OptionDescription
-h, --helpDisplay help message. No filepath is needed to display the help message.

Example Commands

craftsman new:api -h
craftsman new:api C:\fullpath\api.yaml
craftsman new:api C:\fullpath\api.yml
craftsman new:api C:\fullpath\api.json

Example

New API Config File

To get things started, you're going to need to scaffold out what you want your initial API configuration to look like in a yaml or json file.

Don't feel pressured to get this exactly correct from the get go. This file is NOT meant to be a concrete implementation of your API, just a starting point that you can build on over time.

So what's this file look like? For a full list of the configuration option details, you can go to the configuration file options, but let's go over a basic yaml example:

First, you need to start with a name for your API:

SolutionName: VetClinic.Api

Then, let's layout the database context:

SolutionName: VetClinic.Api
DbContext:
  ContextName: VetClinicContext
  DatabaseName: VetClinic
  Provider: SqlServer

Then, we'll add an entity (check out the full list of entity options):

SolutionName: VetClinic.Api
DbContext:
  ContextName: VetClinicContext
  DatabaseName: VetClinic
  Provider: SqlServer
Entities:
  - Name: Pet
    Properties:
    - Name: PetId
      IsPrimaryKey: true
      Type: int
      CanFilter: false
    - Name: Name
      Type: string
      CanFilter: false
    - Name: Type
      Type: string
      CanFilter: false
      CanSort: true

Note that this is a list, so we can add more than one entity if we want:

SolutionName: VetClinic.Api
DbContext:
  ContextName: VetClinicContext
  DatabaseName: VetClinic
  Provider: SqlServer
Entities:
  - Name: Pet
    Properties:
    - Name: PetId
      IsPrimaryKey: true
      Type: int
      CanFilter: false
    - Name: Name
      Type: string
      CanFilter: false
    - Name: Type
      Type: string
      CanFilter: false
      CanSort: true
  - Name: City
    Plural: Cities
    Properties:
    - Name: CityId
      CanManipulate: false
      Type: int
      CanFilter: true
    - Name: Name
      Type: string
      CanFilter: false  

And that's it! Now that we've laid out what we want our API to look like, let's make it!

Using the Command

  1. To actually create our API, you'll want to cd into whatever directory you want to add your project to:

    cd C:\MyFull\RepoPath\Here
  2. Then, let's make sure that our Foundation.Api template and Craftsman CLI are up to date:

    dotnet new foundation.api --update-apply
    dotnet tool update -g craftsman
  3. Then we just need to add our yaml or json path to our craftsman new:api command:

    craftsman new:api C:\Users\Paul\Documents\ApiConfigs\VetClinic.yaml
  4. Once you've run this command, your API should be ready to go!