Getting Started
CLI
Request handling
- Routing
- Action Controller
- Resources
- Context
- Request Binding
- Middleware
- Error Handling
- Sessions
- Cookies
Frontend
Database
- Getting started with Pop
- Soda CLI
- Database Configuration
- Buffalo Integration
- Models
- Generators
- Migrations
- Fizz
- Mutations
- Querying
- Raw Queries
- Callbacks
- Scoping
- Associations and Relationships
- One to one associations
- One to many associations
Guides
- API Applications
- File Uploads
- Background Job Workers
- Mailers
- Tasks
- Local Authentication
- Third Party Authentication
- Events
- Go Modules
- Localization
- Logging
- Template Engines
- Testing
- Videos
Deploy
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{},
})
}