API
- Iframe API
Iframe API
Get API Key​
The package requires an API key to be used. To get one use API to create a "all" scoped API key.
bash
curl -X 'POST' \'https://api.unionavatars.com/v2/keys' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"description": "My Application API Key","scope": "all","expire_at": "<an expiry date>"}'
bash
curl -X 'POST' \'https://api.unionavatars.com/v2/keys' \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"description": "My Application API Key","scope": "all","expire_at": "<an expiry date>"}'
After that save the API Key to be reused later for the Iframe library.
Objects​
In the package the TS defintions to help development process are included.
Options​
ts
import { typeOptions } from '@unionavatars/iframe'Âconstoptions = {color : 'violet', /*string, any valid web color, could be hex code, hsla, or any valid css color unit.Values like 'rgb(255, 120, 80)' or 'hsl(23, 80%, 40%)', or '#E3256B' are allowed*/logo : 'https://example.com/path/to-image.jpg', /*string, any accessible file or dataurl, files over netmust be referenced with an absolute path.*/background : 'https://example.com/path/to-large-image.png', /*string, any accessible file or dataurl, files over netmust be referenced with an absolute path.*/apiKey : '<your-API-key>', // string, required, your API Key.hideLogout : false // boolean, if set hides the logout button} asOptions
ts
import { typeOptions } from '@unionavatars/iframe'Âconstoptions = {color : 'violet', /*string, any valid web color, could be hex code, hsla, or any valid css color unit.Values like 'rgb(255, 120, 80)' or 'hsl(23, 80%, 40%)', or '#E3256B' are allowed*/logo : 'https://example.com/path/to-image.jpg', /*string, any accessible file or dataurl, files over netmust be referenced with an absolute path.*/background : 'https://example.com/path/to-large-image.png', /*string, any accessible file or dataurl, files over netmust be referenced with an absolute path.*/apiKey : '<your-API-key>', // string, required, your API Key.hideLogout : false // boolean, if set hides the logout button} asOptions
This object is used as a parameter sent to the setup function call.
client​
The client is the instance used to communicate with the iframe. It is composed of 4 functions.
- client.on(key, callback)
- client.once(key, callback)
- client.off(key, callback)
- client.request(key, callback)
The on
, once
and off
methods can be called with any key from Outputs
and return with be it's value.
ts
// @filename: main.tsimport {setup } from '@unionavatars/iframe'Âconstiframe =document .querySelector ('iframe#id') asHTMLIFrameElement export constclient =setup (iframe , {ÂapiKey : 'YOUR-API-KEY'})
ts
// @filename: main.tsimport {setup } from '@unionavatars/iframe'Âconstiframe =document .querySelector ('iframe#id') asHTMLIFrameElement export constclient =setup (iframe , {ÂapiKey : 'YOUR-API-KEY'})
Response​
Response object contains a boolean named succeed
which contains true if it completed without errors, and if it failed an error
field will be populated.
ts
import {client } from './main'Âconstres = awaitclient .request ('outfits')
ts
import {client } from './main'Âconstres = awaitclient .request ('outfits')
Functions​
setup​
The setup method is the one responsible to connect the iframe with the container.
Returns a client
object.
It accepts 2 parameters, a required iframe loaded instance and an Options.
ts
import {setup } from '@unionavatars/iframe'
ts
import {setup } from '@unionavatars/iframe'
client.on(key, callback)​
The on
method can be used to listen for events happening on the iframe.
It receives 2 parameters:
- key: the name of the event intercepted
- callback: The callback to listen for the event
ts
import {client } from './main'Âconstlistener = () =>console .log ('logged in')client .on ('logged-in',listener ) // any listen event
ts
import {client } from './main'Âconstlistener = () =>console .log ('logged in')client .on ('logged-in',listener ) // any listen event
avatar-select​
Whenever an avatar has been selected: callback is called with an avatar instance.
ts
import {client } from './main'Âclient .on ('avatar-select',avatar =>console .log (avatar .name ))
ts
import {client } from './main'Âclient .on ('avatar-select',avatar =>console .log (avatar .name ))
camera-ready​
Fired as soon as the camera is available.
ts
import {client } from './main'client .on ('camera-ready', () =>console .log ('camera is ready'))
ts
import {client } from './main'client .on ('camera-ready', () =>console .log ('camera is ready'))
logged-in​
Fired just after any login/signup action.
ts
import {client } from './main'client .on ('logged-in', () =>console .log ('user has been logged in'))
ts
import {client } from './main'client .on ('logged-in', () =>console .log ('user has been logged in'))
avatar-created​
Fired after finishing the creation of an avatar.
ts
import {client } from './main'client .on ('avatar-created',avatar =>console .log (avatar .name ))
ts
import {client } from './main'client .on ('avatar-created',avatar =>console .log (avatar .name ))
page-loaded​
Fired after loading the page.
ts
import {client } from './main'client .on ('page-loaded',view =>console .log (`View ${view } has been loaded`))
ts
import {client } from './main'client .on ('page-loaded',view =>console .log (`View ${view } has been loaded`))
client.once(key, callback)​
The once
behaves the sames as the on
but will be disconnected just after executing the callback.
ts
import {client } from './main'Âclient .once
ts
import {client } from './main'Âclient .once
client.off(key, callback)​
This method disconnected the callback from being executed after the key
event happens.
Consider once
as a shorthand for.
ts
import { typeAvatar } from '@unionavatars/iframe'import {client } from './main'Âconstcb = (avatar :Avatar ) => {console .log (avatar .name )client .off ('avatar-select',cb )} // this is the same as 'once'client .on ('avatar-select',cb )
ts
import { typeAvatar } from '@unionavatars/iframe'import {client } from './main'Âconstcb = (avatar :Avatar ) => {console .log (avatar .name )client .off ('avatar-select',cb )} // this is the same as 'once'client .on ('avatar-select',cb )
client.request(key, callback)​
This method sends a request to the iframe and gets back a response.
ts
import {client } from './main'Âclient .request
ts
import {client } from './main'Âclient .request
login​
Accepts 2 parameters, the user and the password, returns the token or an Error if failed.
ts
import {client } from './main'Âconstresp = awaitclient .request ('login', 'user@email.com', 'some-password')
ts
import {client } from './main'Âconstresp = awaitclient .request ('login', 'user@email.com', 'some-password')
logout​
No parameters, returns a boolean with true on success and false on failure.
ts
import {client } from './main'Âconstresp = awaitclient .request ('logout')
ts
import {client } from './main'Âconstresp = awaitclient .request ('logout')
register​
Accepts 2 parameters, the user and the password, returns the token.
ts
import {client } from './main'Âconstresp = awaitclient .request ('register', 'user@email.com', 'some-password')
ts
import {client } from './main'Âconstresp = awaitclient .request ('register', 'user@email.com', 'some-password')
authenticate​
Accepts a token as a parameter, returns a boolean with true on success and false on failure.
ts
import {client } from './main'Âconstresp = awaitclient .request ('authenticate', 'a-session-token')
ts
import {client } from './main'Âconstresp = awaitclient .request ('authenticate', 'a-session-token')
recover-password​
Accepts an email as parameter, returns a boolean with true on success and false on failure.
ts
import {client } from './main'Âconstresp = awaitclient .request ('recover-password', 'user@email.com')
ts
import {client } from './main'Âconstresp = awaitclient .request ('recover-password', 'user@email.com')
User needs to be authenticated for this to work
reset-password​
Accepts a token and a password as parameters, returns a boolean with true on success and false on failure.
ts
import {client } from './main'Âconstresp = awaitclient .request ('reset-password', 'a-session-token', 'password')
ts
import {client } from './main'Âconstresp = awaitclient .request ('reset-password', 'a-session-token', 'password')
User needs to be authenticated for this to work
take-picture​
Captures the picture on the webcam if available
This action won't work if:
- View is not set to "CreateAvatar"
- Webcam stream is blocked or missing
ts
import {client } from './main'Âconstresp = awaitclient .request ('take-picture')
ts
import {client } from './main'Âconstresp = awaitclient .request ('take-picture')
send-picture​
Sends a picture so it's used instead of the webcam capture
The value to be sent should be the encoded contents of a file, usually a base64 encoded string.
ts
import {client } from './main'Âconstresp = awaitclient .request ('send-picture', 'data:image/jpeg;base64,....')
ts
import {client } from './main'Âconstresp = awaitclient .request ('send-picture', 'data:image/jpeg;base64,....')
confirm-picture​
To confirm the selection on the preview.
This won't work if preview on the CreateAvatar is not set.
ts
import {client } from './main'Âconstresp = awaitclient .request ('confirm-picture')
ts
import {client } from './main'Âconstresp = awaitclient .request ('confirm-picture')
convert-avatar​
Converts the avatar to VRM format. The parameter is the ID of the avatar to convert from GLB to VRM
ts
import {client } from './main'ÂconstavatarId = '22f58d93-0ad9-43c5-a75b-e0c224349f13'Âconstresp = awaitclient .request ('convert-avatar',avatarId )
ts
import {client } from './main'ÂconstavatarId = '22f58d93-0ad9-43c5-a75b-e0c224349f13'Âconstresp = awaitclient .request ('convert-avatar',avatarId )
complete-avatar​
The action to finish the avatar creation with the body selection.
This action won't work if:
- The head has not been created
- The selected view is not the "SelectBody"
ts
import {client } from './main'ÂconstavatarId = '22f58d93-0ad9-43c5-a75b-e0c224349f13'Âconstresp = awaitclient .request ('complete-avatar', 'body-id')
ts
import {client } from './main'ÂconstavatarId = '22f58d93-0ad9-43c5-a75b-e0c224349f13'Âconstresp = awaitclient .request ('complete-avatar', 'body-id')
create-avatar​
Straightforward call to create an avatar omitting the web flow.
It requires:
- A name for the avatar to create
- A "File" object or a head id
- A body id which could be requested with the "bodies" method.
ts
import {client } from './main'ÂconstimageData = 'data:image/jpeg;base64,....'constresp = awaitclient .request ('create-avatar', 'Avatar-name',imageData , 'body-id')
ts
import {client } from './main'ÂconstimageData = 'data:image/jpeg;base64,....'constresp = awaitclient .request ('create-avatar', 'Avatar-name',imageData , 'body-id')
avatars​
No parameters, returns a list of avatars
ts
import {client } from './main'Âconstresp = awaitclient .request ('avatars')if (resp .succeed ) {const {data :avatars } =resp }
ts
import {client } from './main'Âconstresp = awaitclient .request ('avatars')if (resp .succeed ) {const {data :avatars } =resp }
outfits​
No parameters, returns a list of outfits
ts
import {client } from './main'Âconstresp = awaitclient .request ('outfits')if (resp .succeed ) {const {data :outfits } =resp }
ts
import {client } from './main'Âconstresp = awaitclient .request ('outfits')if (resp .succeed ) {const {data :outfits } =resp }
goto​
Accepts a path as a parameter, navigates to that location.
ts
import {client } from './main'Âconstresp = awaitclient .request ('goto', '/register') // Navigates to the register page
ts
import {client } from './main'Âconstresp = awaitclient .request ('goto', '/register') // Navigates to the register page
set-logo​
Accepts a string as a parameter, absolute path to an image or a data-url.
ts
import {client } from './main'Âconstresp = awaitclient .request ('set-logo', 'https://example.com/path/to-image.jpg') // Logo will be updated
ts
import {client } from './main'Âconstresp = awaitclient .request ('set-logo', 'https://example.com/path/to-image.jpg') // Logo will be updated
set-background​
Accepts a string as a parameter, absolute path to an image or a data-url.
ts
import {client } from './main'Âconstresp = awaitclient .request ('set-background', 'https://example.com/path/to-large-image.png') // Background will be updated
ts
import {client } from './main'Âconstresp = awaitclient .request ('set-background', 'https://example.com/path/to-large-image.png') // Background will be updated
set-color​
Accepts a string as a parameter, any valid css color unit.
ts
import {client } from './main'Âconstresp = awaitclient .request ('set-color', '#DEFACE') // Theme color will be updated
ts
import {client } from './main'Âconstresp = awaitclient .request ('set-color', '#DEFACE') // Theme color will be updated
Frameworks​
Basic framework support is included in the API.
the framework utilities are scoped inside its namespace.
ts
import {Iframe ,Provider ,useIframeContext } from '@unionavatars/iframe/react'
ts
import {Iframe ,Provider ,useIframeContext } from '@unionavatars/iframe/react'
Same goes for vue
ts
import {Plugin ,Iframe ,useIframeContext } from '@unionavatars/iframe/vue'
ts
import {Plugin ,Iframe ,useIframeContext } from '@unionavatars/iframe/vue'
React​
Provider​
The base provider that connects the client to the iframe on load.
The provider accepts several optional parameter for initialization and a required apiKey
parameter
ts
import {Iframe ,Provider } from '@unionavatars/iframe/react'importApplication from './app'ÂfunctionRoot () {return ( // this parameters will set the base color and the log<Provider background ="purple"color ="blue"logo ="https://picsum.photos/100/80"apiKey ="YOUR-API-KEY"hideLogout ><Iframe src ="https://iframe.unionavatars.com" /><Application /></Provider >)}
ts
import {Iframe ,Provider } from '@unionavatars/iframe/react'importApplication from './app'ÂfunctionRoot () {return ( // this parameters will set the base color and the log<Provider background ="purple"color ="blue"logo ="https://picsum.photos/100/80"apiKey ="YOUR-API-KEY"hideLogout ><Iframe src ="https://iframe.unionavatars.com" /><Application /></Provider >)}
Iframe​
Convenience wrapper to connect the onload event with the Provider
ts
import {Iframe ,Provider } from '@unionavatars/iframe/react'importApplication from './app'ÂfunctionRoot () {return ( // this parameters will set the base color and the log<Provider apiKey ="YOUR-API-KEY"><Iframe src ="https://iframe.unionavatars.com" /><Application /></Provider >)}
ts
import {Iframe ,Provider } from '@unionavatars/iframe/react'importApplication from './app'ÂfunctionRoot () {return ( // this parameters will set the base color and the log<Provider apiKey ="YOUR-API-KEY"><Iframe src ="https://iframe.unionavatars.com" /><Application /></Provider >)}
useIframeContext​
Hook to get the client
to interact with the iframe
ts
import {useIframeContext } from '@unionavatars/iframe/react'Âconst {eventer } =useIframeContext ()
ts
import {useIframeContext } from '@unionavatars/iframe/react'Âconst {eventer } =useIframeContext ()
Vue​
Plugin​
Plugin to connect the vue application and the iframe.
ts
import {ÂcreateApp } from 'vue'import {Plugin } from '@unionavatars/iframe/vue'Âconstroot =document .querySelector ('#root') asHTMLElement constapp =createApp (root )app .use (Plugin ({ÂapiKey : 'YOUR-API-KEY' }))Â
ts
import {ÂcreateApp } from 'vue'import {Plugin } from '@unionavatars/iframe/vue'Âconstroot =document .querySelector ('#root') asHTMLElement constapp =createApp (root )app .use (Plugin ({ÂapiKey : 'YOUR-API-KEY' }))Â
Iframe​
Convenience wrapper to connect the onload event with the Plugin
ts
import {ÂdefineComponent ,h } from 'vue'import {Iframe } from '@unionavatars/iframe/react'Âconstcomponent =defineComponent ({render () {return () =>h (Iframe , {src : "https://iframe.unionavatars.com" })}})
ts
import {ÂdefineComponent ,h } from 'vue'import {Iframe } from '@unionavatars/iframe/react'Âconstcomponent =defineComponent ({render () {return () =>h (Iframe , {src : "https://iframe.unionavatars.com" })}})
useIframeContext​
Hook to get the context that contains the client
useful to interact with the iframe.
ts
import {useIframeContext } from '@unionavatars/iframe/vue'Âconstctx =useIframeContext () // ctx.eventer
ts
import {useIframeContext } from '@unionavatars/iframe/vue'Âconstctx =useIframeContext () // ctx.eventer