logo Buffalo slack logo
API Applications
Guides

API Applications

Applications that only serve API end-points, typically JSON, are very different from those that serve HTML, JavaScript, and CSS. In this guide, you’ll learn how to build an API-only app, using Buffalo.

Creating a New API Application

When creating a new Buffalo application using the buffalo new command, the optional --api flag will generate an application that is better suited to serving APIs than a stock Buffalo application.

$ buffalo new coke --api

Slimmed Project Layout

Applications generated with the --api flag don’t contain any front systems. This means there is no templating, stylesheets, etc…

$ buffalo new coke --api
β”œβ”€β”€ actions/
β”‚	β”œβ”€β”€ app.go
β”‚	└── render.go
β”œβ”€β”€ cmd/
β”‚	└── app/
β”‚		└── main.go
β”œβ”€β”€ config/
β”œβ”€β”€ fixtures/
β”œβ”€β”€ grifts/
β”œβ”€β”€ locales/
β”œβ”€β”€ models/
β”œβ”€β”€ .buffalo.dev.yml
β”œβ”€β”€ .codeclimate.yml
β”œβ”€β”€ .docketignore
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”œβ”€β”€ database.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ inflections.json
β”œβ”€β”€ README.md
$ buffalo new coke
β”œβ”€β”€ .yarn/
β”œβ”€β”€ actions/
β”‚	β”œβ”€β”€ app.go
β”‚	└── render.go
β”œβ”€β”€ assets/
β”œβ”€β”€ cmd/
β”‚	└── app/
β”‚		└── main.go
β”œβ”€β”€ config/
β”œβ”€β”€ fixtures/
β”œβ”€β”€ grifts/
β”œβ”€β”€ locales/
β”œβ”€β”€ models/
β”œβ”€β”€ public/
β”œβ”€β”€ templates/
β”œβ”€β”€ .babelrc
β”œβ”€β”€ .buffalo.dev.yml
β”œβ”€β”€ .codeclimate.yml
β”œβ”€β”€ .docketignore
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .pnp.cjs
β”œβ”€β”€ .pnp.loader.mjs
β”œβ”€β”€ .yarnrc.yml
β”œβ”€β”€ database.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ inflections.json
β”œβ”€β”€ package.json
β”œβ”€β”€ postcss.config.js
β”œβ”€β”€ README.md
β”œβ”€β”€ webpack.config.js
└── yarn.lock

Tuned actions/app.go actions/render.go Files

API applications have actions/app.go and actions/render.go files that are a good starting point for API applications.

$ buffalo new coke --api
func App() *buffalo.App {
	if app == nil {
		app = buffalo.New(buffalo.Options{
			Env:          ENV,
			SessionStore: sessions.Null{},
			PreWares: []buffalo.PreWare{
				cors.Default().Handler,
			},
			SessionName: "_coke_session",
		})
		app.Use(forceSSL())
		app.Use(paramlogger.ParameterLogger)
		app.Use(contenttype.Set("application/json"))

		app.Use(popmw.Transaction(models.DB))
		app.GET("/", HomeHandler)
	}

	return app
}
func init() {
	r = render.New(render.Options{
		DefaultContentType: "application/json",
	})
}
$ buffalo new coke
func App() *buffalo.App {
	if app == nil {
		app = buffalo.New(buffalo.Options{
			Env:         ENV,
			SessionName: "_coke_session",
		})
		app.Use(forceSSL())
		app.Use(paramlogger.ParameterLogger)
		app.Use(csrf.New)
		app.Use(popmw.Transaction(models.DB))
		app.Use(translations())
		app.GET("/", HomeHandler)
		app.ServeFiles("/", http.FS(public.FS())) // serve files from the public directory
	}

	return app
}
func init() {
	r = render.New(render.Options{
		HTMLLayout: "application.plush.html",
		TemplatesFS: templates.FS(),
		AssetsFS: public.FS(),
		Helpers: render.Helpers{},
	})
}