Create a Store
A store is a OpenFGA entity that contains your authorization data. You will need to create a store in OpenFGA before adding an authorization model and relationship tuples to it.
This article explains how to setup an OpenFGA store.
Step By Step
- Node.js
- Go
- .NET
- Python
- CLI
- curl
const { OpenFgaClient } = require('@openfga/sdk'); // OR import { OpenFgaClient } from '@openfga/sdk';
const openFga = new OpenFgaClient({
apiScheme: process.env.FGA_API_SCHEME, // optional, defaults to "https"
apiHost: process.env.FGA_API_HOST // required, define without the scheme (e.g. api.openfga.example instead of https://api.openfga.example)
});
const { id: storeId } = await openFga.createStore({
name: "FGA Demo Store",
});
import (
openfga "github.com/openfga/go-sdk"
. "github.com/openfga/go-sdk/client"
"os"
)
func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId: openfga.PtrString(os.Getenv("FGA_MODEL_ID")), // optional, can be overridden per request
})
if err != nil {
// .. Handle error
}
resp, err := fgaClient.CreateStore(context.Background()).Body(ClientCreateStoreRequest{Name: "FGA Demo"}).Execute()
if err != nil {
// .. Handle error
}
}
using OpenFga.Sdk.Client;
using OpenFga.Sdk.Client.Model;
using OpenFga.Sdk.Model;
using Environment = System.Environment;
namespace ExampleApp;
class MyProgram {
static async Task Main() {
var configuration = new ClientConfiguration() {
ApiScheme = Environment.GetEnvironmentVariable("FGA_API_SCHEME"), // optional. Can be "http" or "https". Defaults to "https"
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId = Environment.GetEnvironmentVariable("FGA_STORE_ID"), // optional, not needed for \`CreateStore\` and \`ListStores\`, required before calling for all other methods
AuthorizationModelId = Environment.GetEnvironmentVariable("FGA_MODEL_ID"), // optional, can be overridden per request
};
var fgaClient = new OpenFgaClient(configuration);
var store = await fgaClient.CreateStore(new ClientCreateStoreRequest(){Name = "FGA Demo"})
{
Name = "FGA Demo Store"
});
}
}
import os
import openfga_sdk
from openfga_sdk.client import OpenFgaClient
from openfga_sdk.models.create_store_request import CreateStoreRequest
configuration = openfga_sdk.Configuration(
scheme = os.environ.get('FGA_API_SCHEME'),
api_host = os.environ.get('FGA_API_HOST'),
)
async with OpenFgaClient(configuration) as fga_client:
body = CreateStoreRequest(
name = "FGA Demo Store",
)
response = await fga_client.create_store(body)
async def create_store():
try:
# Create a store
body = CreateStoreRequest(
name = "FGA Demo",
)
api_response = await fga_client_instance.create_store(body)
except openfga_sdk.ApiException as e:
print("Exception when calling OpenFgaClient->create_store: %s\n" % e)
fga store create --name "FGA Demo Store"
# To create the store and directly put the Store ID into an env variable:
# export FGA_STORE_ID=$(fga store create --name "FGA Demo Store" | jq -r .id)
curl -X POST $FGA_API_HOST/stores \
-H "content-type: application/json" \
-d '{"name": "FGA Demo Store"}'