Budibase/budibase · error · HTTPError

Custom REST templates can only be associated with REST datas

Error message

Custom REST templates can only be associated with REST datasources

What it means

During `_import`, after verifying the template exists, the target datasource is fetched and its `source` must equal `SourceName.REST`. Associating a custom REST (OpenAPI) template with a datasource of any other type (e.g. PostgreSQL, MongoDB) throws this 400 HTTPError, since template import only makes sense for REST datasources.

Source

Thrown at packages/server/src/api/controllers/query/index.ts:149

      },
    })
    await saveDatasource(datasourceCtx)
    datasourceId = datasourceCtx.body.datasource._id
  } else {
    if (body.restTemplateId) {
      await sdk.restTemplates.withCustomRestTemplateLock({
        resource: body.restTemplateId,
        task: async () => {
          const templateExists = await sdk.restTemplates.exists(
            body.restTemplateId!
          )
          if (!templateExists) {
            throw new HTTPError("Custom REST template not found", 404)
          }

          const datasource = await sdk.datasources.get(body.datasourceId!)
          if (datasource.source !== SourceName.REST) {
            throw new HTTPError(
              "Custom REST templates can only be associated with REST datasources",
              400
            )
          }
          importer.prepareDatasourceConfig(datasource)
          datasource.restTemplateId = body.restTemplateId
          const response = await context
            .getWorkspaceDB()
            .put(sdk.tables.populateExternalTableSchemas(datasource))
          datasource._rev = response.rev
          await events.datasource.updated(datasource)
          builderSocket?.emitDatasourceUpdate(ctx, datasource)
        },
      })
    }
    // use existing datasource
    datasourceId = body.datasourceId
  }

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Create or select a REST datasource and use its `datasourceId` in the request
  2. Verify `datasource.source === "REST"` before calling the import endpoint
  3. Check that the datasourceId is not from a different app or resource type

Example fix

// before
await importRestTemplate({ restTemplateId: id, datasourceId: pgDatasource._id })
// after
const ds = await getDatasource(restDatasourceId)
if (ds.source !== "REST") throw new Error("Need a REST datasource")
await importRestTemplate({ restTemplateId: id, datasourceId: restDatasource._id })
Defensive patterns

Strategy: validation

Validate before calling

const ds = await getDatasource(datasourceId)
if (ds.source !== "REST") {
  throw new Error("Custom REST templates require a REST datasource")
}

Type guard

const isRestDatasource = (ds) => ds.source === "REST"

Try / catch

try {
  await importRestTemplate({ restTemplateId, datasourceId })
} catch (e) {
  if (e.status === 400 && String(e.message).includes("only be associated with REST datasources")) {
    // prompt user to pick a REST datasource
  }
}

Prevention

When it happens

Trigger: Calling the import endpoint with a `datasourceId` pointing to a non-REST datasource (SQL, Mongo, etc.) while also passing a `restTemplateId`.

Common situations: Wrong datasource selected in the import dialog; mixing up datasource IDs between apps; scripting the import against the wrong ID; UI bug passing the wrong resource to the request.

Related errors


AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29). Data as JSON: /api/errors/1bf520d042c9423d. Report an issue: GitHub.