How to create our own API in Apigee

Not applicable
 
0 3 163
3 REPLIES 3

Anu ... You can use this tutorial as reference http://docs.apigee.com/tutorials/add-and-configure-your-first-api

Hope this helps.

Not applicable

Hi @rakeshapi

In this tutorial we are created API Proxy for the already exited API .My question is can we create our own API Like as IBM API management.If it possible how can we create?

adas
New Member

@A.Anu Manasa You can create your own apis using node.js. You can write a node.js script and run that in Apigee Edge. Here's an example: http://docs.apigee.com/api-services/content/getting-started-nodejs-apigee-edge

Let's say below is a simple node.js script to echo a http request or return different responses based on the path:

var http = require('http');
var Router = require('node-router');
var url = require('url') ;
var requestIp = require('request-ip');
var js2xmlparser = require("js2xmlparser");


const PORT=8080; 


var router = Router();    
var route = router.push;


route('GET', 'POST', '/echo', echoRouteHandler);
route('/hello', helloRouteHandler);  
route(helloRouteHandler); 
route(errorHandler); 


var server = http.createServer(router);


server.listen(PORT, function(){
    console.log("Server listening on: http://localhost:%s", PORT);
});


function errorHandler(err, req, res, next) {
  res.writeHead(500);
  res.end(err);
}


function helloRouteHandler(req, res, next) {
    if (true){
        res.writeHead(200, {'Content-Type': 'text/plain', 'Powered-By': 'Apigee'});
        var queryObject = url.parse(req.url,true).query;
        var user = typeof queryObject.user !== 'undefined' ? queryObject.user : 'Guest User';
        res.end('HELLO ' + user + '!!!'); 
    }
    else next();
}

function echoRouteHandler(req, res, next) {
    if (true){
        var body = [];
        req.on('data', function(chunk) {
        body.push(chunk);
        }).on('end', function() {
        body = Buffer.concat(body).toString();
        });
        var headers = req.headers;
        var method = req.method;
        var url = req.url;
        var responseBody = {
          headers: headers,
          method: method,
          url: url,
          body: body
        };
        res.writeHead(200, {'Content-Type': 'application/json', 'Powered-By': 'Apigee'});
        res.write(JSON.stringify(responseBody));
        res.end();
    }
    else next();
}

You could take this and run it as an API on Apigee Edge. Here's the complete apiproxy bundle with the packaged node_modules.nodeapp-v1-rev1-2016-04-22.zip