Encore Tutorial

Not quite sure whether Encore is right for you? Here's a whirlwind tour of the concepts of Encore.

If you want to follow along by building an app, check out the tutorial.

Running your app

You create your app with encore app create my-app. You run it with encore run. That's it.

Creating an API endpoint

An API endpoint with Encore consists of two data structures (request data, response data) and a function. It looks like this:

type FooParams struct {
    Name string
}

type FooResponse struct {
    Message string
}

// Foo is an example endpoint.
// encore:api public
func Foo(ctx context.Context, params *FooParams) (*FooResponse, error) {
    message := "Hello, " + input.Name
    return &FooResponse{Message: message}, nil
}

Calling an API endpoint

You can either call endpoints internally from within your Encore application, or externally.

To call an endpoint from another Encore endpoint or service, treat it as if it were a function call:

func someFunc(ctx context.Context) (message string, err error) {
    resp, err := myservice.Foo(ctx, &myservice.FooParams{
        Name: "Jane",
    })
    if err != nil {
        return "", err
    }
    return resp.Message, nil // returns "Hello, Jane"
}

To call a public endpoint from outside of Encore, perform an HTTP+JSON request:

$ curl https://my-app.encoreapi.com/production/myservice.Foo \
-d '{"Name": "John"}'

And you get a JSON response back:

{"Message":"Hello, John"}

Creating a database

It's really easy to set up an SQL database with Encore.

First create an SQL migration:

CREATE TABLE people (
    name TEXT PRIMARY KEY,
    meeting_count INT NOT NULL
);

Then import encore.dev/storage/sqldb in your service and use one of the provided functions, like sqldb.QueryRow or sqldb.Exec to make queries.

There's no need to worry about creating the database, migrating it, connecting to it, managing passwords, or backing it up. We've got it covered.

Trying it out yourself

Does this seem compelling? Try it out for yourself by heading over to the Quick install guide.

Subscribe to our Engineering Blog

The latest ideas to help you build better software.

You can unsubscribe at any time.

© 2020 Encore. All rights reserved.