Candle Documentation

RestAPI

Set up your own RestAPI

Wick provides an easy solution for RestAPIs as well. To set one up, let’s start with a demo.wick file. This is a standard Wick application config file.

To start off, like all .wick files, we declare the name and kind of our app/component.

kind: wick/app@v1
name: rest_demo

Wick sets up Rest APIs as HTTP triggers. In order to use this, we need to declare an HTTP resource for our app. We can set the port that will be exposed and the address. (0.0.0.0 binds to localhost)

resources:
  - name: http
    resource:
      kind: wick/resource/tcpport@v1
      port: 8999
      address: 0.0.0.0

Now that we have declared our application’s needs, we can set up REST triggers .

triggers:
  - kind: wick/trigger/http@v1
    resource: http
    routers:
      - kind: wick/router/rest@v1
        path: /api/
        routes:

Our trigger is set up using the HTTP resource we provided to our app above. Under routers , we have our REST router with the ability to set its path and routes.

Routes is where we get to add and define our REST APIs. Let’s take a look at the structure of Wick REST routes.

routes:
  - uri: 'desired URL endpoint'
    operation: <component name>::<operation name>
    methods:
      - POST

With those three fields, we have set a URI destination and assigned an operation to take place when we hit that URI. We can add as many routes as desired following this same structure.

Done!

Here is what your complete demo.wick file would look like:

kind: wick/app@v1
name: rest_demo
resources:
  - name: http
    resource:
      kind: wick/resource/tcpport@v1
      port: 8999
      address: 0.0.0.0
triggers:
  - kind: wick/trigger/http@v1
    resource: http
    routers:
      - kind: wick/router/rest@v1
        path: /api/
        routes:
          - uri: 'desired URL endpoint'
            operation: <component name>::<operation name>
            methods:
              - POST

This is essentially all you need to create REST API routes in Wick. To see how it all works, check out our HTTP example applications here.