Skip to content
Marlon Carvalho edited this page Jul 20, 2015 · 9 revisions

Darter is available as a Pub package at Pub. Lets create our first API!

  • Create a new directory named myAPI.
  • In this directory, create a file named pubspec.yaml and put the code below inside it.
name: beer
version: 0.0.1
author: Your Name
description: API for exposing beer data.
homepage: Your Homepage
environment:
  sdk: '>=1.8.3 <2.0.0'
dependencies:
  collection: '>=1.1.1 <2.0.0'
  darter: '0.0.1.beta'
  • Run pub get to update your project dependencies
  • Create a file named main.dart in the project's main directory.
  • Add the following code to this file.
library beer;

import 'package:darter/darter.dart';

@API(path: 'beers')
class BeerAPI { 

 @GET()
 List get() {
    return ["Beer 1", "Beer 2"];
  }

}

main() {
  new DarterServer()
    ..addApi(new BeerAPI())
    ..start();
}

We've just created our first API using Darter. Easy, right? Now we need to run our server and test if everything is okay. In the command line, type dart main.dart then open your favorite browser and point it to the address http://localhost:8080/beers. You'll see a JSON Array containing two Strings.

Now, go the the next page to understand what we've done here.

Clone this wiki locally